blob: 862ef30853d32c0520df97559150f5406d2f17e2 [file] [log] [blame]
David Daney6b52c002012-08-22 12:25:07 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2011, 2012 Cavium, Inc.
7 */
8
9#include <linux/platform_device.h>
10#include <linux/interrupt.h>
11#include <linux/spi/spi.h>
12#include <linux/module.h>
13#include <linux/delay.h>
14#include <linux/init.h>
15#include <linux/io.h>
16#include <linux/of.h>
17
18#include <asm/octeon/octeon.h>
19#include <asm/octeon/cvmx-mpi-defs.h>
20
21#define OCTEON_SPI_CFG 0
22#define OCTEON_SPI_STS 0x08
23#define OCTEON_SPI_TX 0x10
24#define OCTEON_SPI_DAT0 0x80
25
26#define OCTEON_SPI_MAX_BYTES 9
27
28#define OCTEON_SPI_MAX_CLOCK_HZ 16000000
29
30struct octeon_spi {
David Daney6b52c002012-08-22 12:25:07 -070031 u64 register_base;
32 u64 last_cfg;
33 u64 cs_enax;
34};
35
36struct octeon_spi_setup {
37 u32 max_speed_hz;
38 u8 chip_select;
39 u8 mode;
40 u8 bits_per_word;
41};
42
43static void octeon_spi_wait_ready(struct octeon_spi *p)
44{
45 union cvmx_mpi_sts mpi_sts;
46 unsigned int loops = 0;
47
48 do {
49 if (loops++)
50 __delay(500);
51 mpi_sts.u64 = cvmx_read_csr(p->register_base + OCTEON_SPI_STS);
52 } while (mpi_sts.s.busy);
53}
54
55static int octeon_spi_do_transfer(struct octeon_spi *p,
56 struct spi_message *msg,
57 struct spi_transfer *xfer,
58 bool last_xfer)
59{
60 union cvmx_mpi_cfg mpi_cfg;
61 union cvmx_mpi_tx mpi_tx;
62 unsigned int clkdiv;
63 unsigned int speed_hz;
64 int mode;
65 bool cpha, cpol;
David Daney6b52c002012-08-22 12:25:07 -070066 const u8 *tx_buf;
67 u8 *rx_buf;
68 int len;
69 int i;
70
71 struct octeon_spi_setup *msg_setup = spi_get_ctldata(msg->spi);
72
73 speed_hz = msg_setup->max_speed_hz;
74 mode = msg_setup->mode;
75 cpha = mode & SPI_CPHA;
76 cpol = mode & SPI_CPOL;
David Daney6b52c002012-08-22 12:25:07 -070077
78 if (xfer->speed_hz)
79 speed_hz = xfer->speed_hz;
David Daney6b52c002012-08-22 12:25:07 -070080
81 if (speed_hz > OCTEON_SPI_MAX_CLOCK_HZ)
82 speed_hz = OCTEON_SPI_MAX_CLOCK_HZ;
83
84 clkdiv = octeon_get_io_clock_rate() / (2 * speed_hz);
85
86 mpi_cfg.u64 = 0;
87
88 mpi_cfg.s.clkdiv = clkdiv;
89 mpi_cfg.s.cshi = (mode & SPI_CS_HIGH) ? 1 : 0;
90 mpi_cfg.s.lsbfirst = (mode & SPI_LSB_FIRST) ? 1 : 0;
91 mpi_cfg.s.wireor = (mode & SPI_3WIRE) ? 1 : 0;
92 mpi_cfg.s.idlelo = cpha != cpol;
93 mpi_cfg.s.cslate = cpha ? 1 : 0;
94 mpi_cfg.s.enable = 1;
95
96 if (msg_setup->chip_select < 4)
97 p->cs_enax |= 1ull << (12 + msg_setup->chip_select);
98 mpi_cfg.u64 |= p->cs_enax;
99
100 if (mpi_cfg.u64 != p->last_cfg) {
101 p->last_cfg = mpi_cfg.u64;
102 cvmx_write_csr(p->register_base + OCTEON_SPI_CFG, mpi_cfg.u64);
103 }
104 tx_buf = xfer->tx_buf;
105 rx_buf = xfer->rx_buf;
106 len = xfer->len;
107 while (len > OCTEON_SPI_MAX_BYTES) {
108 for (i = 0; i < OCTEON_SPI_MAX_BYTES; i++) {
109 u8 d;
110 if (tx_buf)
111 d = *tx_buf++;
112 else
113 d = 0;
114 cvmx_write_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i), d);
115 }
116 mpi_tx.u64 = 0;
117 mpi_tx.s.csid = msg_setup->chip_select;
118 mpi_tx.s.leavecs = 1;
119 mpi_tx.s.txnum = tx_buf ? OCTEON_SPI_MAX_BYTES : 0;
120 mpi_tx.s.totnum = OCTEON_SPI_MAX_BYTES;
121 cvmx_write_csr(p->register_base + OCTEON_SPI_TX, mpi_tx.u64);
122
123 octeon_spi_wait_ready(p);
124 if (rx_buf)
125 for (i = 0; i < OCTEON_SPI_MAX_BYTES; i++) {
126 u64 v = cvmx_read_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i));
127 *rx_buf++ = (u8)v;
128 }
129 len -= OCTEON_SPI_MAX_BYTES;
130 }
131
132 for (i = 0; i < len; i++) {
133 u8 d;
134 if (tx_buf)
135 d = *tx_buf++;
136 else
137 d = 0;
138 cvmx_write_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i), d);
139 }
140
141 mpi_tx.u64 = 0;
142 mpi_tx.s.csid = msg_setup->chip_select;
143 if (last_xfer)
144 mpi_tx.s.leavecs = xfer->cs_change;
145 else
146 mpi_tx.s.leavecs = !xfer->cs_change;
147 mpi_tx.s.txnum = tx_buf ? len : 0;
148 mpi_tx.s.totnum = len;
149 cvmx_write_csr(p->register_base + OCTEON_SPI_TX, mpi_tx.u64);
150
151 octeon_spi_wait_ready(p);
152 if (rx_buf)
153 for (i = 0; i < len; i++) {
154 u64 v = cvmx_read_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i));
155 *rx_buf++ = (u8)v;
156 }
157
158 if (xfer->delay_usecs)
159 udelay(xfer->delay_usecs);
160
161 return xfer->len;
162}
163
David Daney6b52c002012-08-22 12:25:07 -0700164static int octeon_spi_transfer_one_message(struct spi_master *master,
165 struct spi_message *msg)
166{
167 struct octeon_spi *p = spi_master_get_devdata(master);
168 unsigned int total_len = 0;
169 int status = 0;
170 struct spi_transfer *xfer;
171
172 /*
173 * We better have set the configuration via a call to .setup
174 * before we get here.
175 */
176 if (spi_get_ctldata(msg->spi) == NULL) {
177 status = -EINVAL;
178 goto err;
179 }
180
181 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Axel Lin0a4e2102014-01-15 13:22:17 +0800182 bool last_xfer = list_is_last(&xfer->transfer_list,
183 &msg->transfers);
David Daney6b52c002012-08-22 12:25:07 -0700184 int r = octeon_spi_do_transfer(p, msg, xfer, last_xfer);
185 if (r < 0) {
186 status = r;
187 goto err;
188 }
189 total_len += r;
190 }
191err:
192 msg->status = status;
193 msg->actual_length = total_len;
194 spi_finalize_current_message(master);
195 return status;
196}
197
198static struct octeon_spi_setup *octeon_spi_new_setup(struct spi_device *spi)
199{
200 struct octeon_spi_setup *setup = kzalloc(sizeof(*setup), GFP_KERNEL);
201 if (!setup)
202 return NULL;
203
204 setup->max_speed_hz = spi->max_speed_hz;
205 setup->chip_select = spi->chip_select;
206 setup->mode = spi->mode;
207 setup->bits_per_word = spi->bits_per_word;
208 return setup;
209}
210
211static int octeon_spi_setup(struct spi_device *spi)
212{
David Daney6b52c002012-08-22 12:25:07 -0700213 struct octeon_spi_setup *new_setup;
214 struct octeon_spi_setup *old_setup = spi_get_ctldata(spi);
215
David Daney6b52c002012-08-22 12:25:07 -0700216 new_setup = octeon_spi_new_setup(spi);
217 if (!new_setup)
218 return -ENOMEM;
219
220 spi_set_ctldata(spi, new_setup);
221 kfree(old_setup);
222
223 return 0;
224}
225
226static void octeon_spi_cleanup(struct spi_device *spi)
227{
228 struct octeon_spi_setup *old_setup = spi_get_ctldata(spi);
229 spi_set_ctldata(spi, NULL);
230 kfree(old_setup);
231}
232
Grant Likelyfd4a3192012-12-07 16:57:14 +0000233static int octeon_spi_probe(struct platform_device *pdev)
David Daney6b52c002012-08-22 12:25:07 -0700234{
David Daney6b52c002012-08-22 12:25:07 -0700235 struct resource *res_mem;
236 struct spi_master *master;
237 struct octeon_spi *p;
238 int err = -ENOENT;
239
240 master = spi_alloc_master(&pdev->dev, sizeof(struct octeon_spi));
241 if (!master)
242 return -ENOMEM;
243 p = spi_master_get_devdata(master);
Axel Line1b18ea2013-08-05 15:53:32 +0800244 platform_set_drvdata(pdev, master);
David Daney6b52c002012-08-22 12:25:07 -0700245
246 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
247
248 if (res_mem == NULL) {
249 dev_err(&pdev->dev, "found no memory resource\n");
250 err = -ENXIO;
251 goto fail;
252 }
253 if (!devm_request_mem_region(&pdev->dev, res_mem->start,
254 resource_size(res_mem), res_mem->name)) {
255 dev_err(&pdev->dev, "request_mem_region failed\n");
256 goto fail;
257 }
258 p->register_base = (u64)devm_ioremap(&pdev->dev, res_mem->start,
259 resource_size(res_mem));
260
261 /* Dynamic bus numbering */
262 master->bus_num = -1;
263 master->num_chipselect = 4;
264 master->mode_bits = SPI_CPHA |
265 SPI_CPOL |
266 SPI_CS_HIGH |
267 SPI_LSB_FIRST |
268 SPI_3WIRE;
269
270 master->setup = octeon_spi_setup;
271 master->cleanup = octeon_spi_cleanup;
David Daney6b52c002012-08-22 12:25:07 -0700272 master->transfer_one_message = octeon_spi_transfer_one_message;
Axel Linf79cc882013-08-11 23:09:43 +0800273 master->bits_per_word_mask = SPI_BPW_MASK(8);
David Daney6b52c002012-08-22 12:25:07 -0700274
275 master->dev.of_node = pdev->dev.of_node;
Jingoo Han22ad2d82013-09-24 13:34:24 +0900276 err = devm_spi_register_master(&pdev->dev, master);
David Daney6b52c002012-08-22 12:25:07 -0700277 if (err) {
278 dev_err(&pdev->dev, "register master failed: %d\n", err);
279 goto fail;
280 }
281
282 dev_info(&pdev->dev, "OCTEON SPI bus driver\n");
283
284 return 0;
285fail:
286 spi_master_put(master);
287 return err;
288}
289
Grant Likelyfd4a3192012-12-07 16:57:14 +0000290static int octeon_spi_remove(struct platform_device *pdev)
David Daney6b52c002012-08-22 12:25:07 -0700291{
Axel Line1b18ea2013-08-05 15:53:32 +0800292 struct spi_master *master = platform_get_drvdata(pdev);
293 struct octeon_spi *p = spi_master_get_devdata(master);
David Daney6b52c002012-08-22 12:25:07 -0700294 u64 register_base = p->register_base;
295
David Daney6b52c002012-08-22 12:25:07 -0700296 /* Clear the CSENA* and put everything in a known state. */
297 cvmx_write_csr(register_base + OCTEON_SPI_CFG, 0);
298
299 return 0;
300}
301
302static struct of_device_id octeon_spi_match[] = {
303 { .compatible = "cavium,octeon-3010-spi", },
304 {},
305};
306MODULE_DEVICE_TABLE(of, octeon_spi_match);
307
308static struct platform_driver octeon_spi_driver = {
309 .driver = {
310 .name = "spi-octeon",
311 .owner = THIS_MODULE,
312 .of_match_table = octeon_spi_match,
313 },
314 .probe = octeon_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000315 .remove = octeon_spi_remove,
David Daney6b52c002012-08-22 12:25:07 -0700316};
317
318module_platform_driver(octeon_spi_driver);
319
320MODULE_DESCRIPTION("Cavium, Inc. OCTEON SPI bus driver");
321MODULE_AUTHOR("David Daney");
322MODULE_LICENSE("GPL");