Merge tag 'staging-5.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-microblaze.git] / drivers / staging / kpc2000 / kpc_spi / spi_driver.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * KP2000 SPI controller driver
4  *
5  * Copyright (C) 2014-2018 Daktronics
6  * Author: Matt Sickler <matt.sickler@daktronics.com>
7  * Very loosely based on spi-omap2-mcspi.c
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/io-64-nonatomic-lo-hi.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/err.h>
19 #include <linux/clk.h>
20 #include <linux/io.h>
21 #include <linux/slab.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/of.h>
24 #include <linux/of_device.h>
25 #include <linux/gcd.h>
26 #include <linux/spi/spi.h>
27 #include <linux/spi/flash.h>
28 #include <linux/mtd/partitions.h>
29
30 #include "../kpc.h"
31 #include "spi_parts.h"
32
33
34 /***************
35  * SPI Defines *
36  ***************/
37 #define KP_SPI_REG_CONFIG 0x0 /* 0x00 */
38 #define KP_SPI_REG_STATUS 0x1 /* 0x08 */
39 #define KP_SPI_REG_FFCTRL 0x2 /* 0x10 */
40 #define KP_SPI_REG_TXDATA 0x3 /* 0x18 */
41 #define KP_SPI_REG_RXDATA 0x4 /* 0x20 */
42
43 #define KP_SPI_CLK           48000000
44 #define KP_SPI_MAX_FIFODEPTH 64
45 #define KP_SPI_MAX_FIFOWCNT  0xFFFF
46
47 #define KP_SPI_REG_CONFIG_TRM_TXRX 0
48 #define KP_SPI_REG_CONFIG_TRM_RX   1
49 #define KP_SPI_REG_CONFIG_TRM_TX   2
50
51 #define KP_SPI_REG_STATUS_RXS   0x01
52 #define KP_SPI_REG_STATUS_TXS   0x02
53 #define KP_SPI_REG_STATUS_EOT   0x04
54 #define KP_SPI_REG_STATUS_TXFFE 0x10
55 #define KP_SPI_REG_STATUS_TXFFF 0x20
56 #define KP_SPI_REG_STATUS_RXFFE 0x40
57 #define KP_SPI_REG_STATUS_RXFFF 0x80
58
59
60
61 /******************
62  * SPI Structures *
63  ******************/
64 struct kp_spi {
65         struct spi_master  *master;
66         u64 __iomem        *base;
67         unsigned long       phys;
68         struct device      *dev;
69         int                 fifo_depth;
70         unsigned int        pin_dir:1;
71 };
72
73
74 struct kp_spi_controller_state {
75     void __iomem   *base;
76     unsigned long   phys;
77     unsigned char   chip_select;
78     int             word_len;
79     s64             conf_cache;
80 };
81
82
83 union kp_spi_config {
84     /* use this to access individual elements */
85     struct __attribute__((packed)) spi_config_bitfield {
86         unsigned int pha       : 1; /* spim_clk Phase      */
87         unsigned int pol       : 1; /* spim_clk Polarity   */
88         unsigned int epol      : 1; /* spim_csx Polarity   */
89         unsigned int dpe       : 1; /* Transmission Enable */
90         unsigned int wl        : 5; /* Word Length         */
91         unsigned int           : 3;
92         unsigned int trm       : 2; /* TxRx Mode           */
93         unsigned int cs        : 4; /* Chip Select         */
94         unsigned int wcnt      : 7; /* Word Count          */
95         unsigned int ffen      : 1; /* FIFO Enable         */
96         unsigned int spi_en    : 1; /* SPI Enable          */
97         unsigned int           : 5;
98     } bitfield;
99     /* use this to grab the whole register */
100     u32 reg;
101 };
102
103
104
105 union kp_spi_status {
106     struct __attribute__((packed)) spi_status_bitfield {
107         unsigned int rx    :  1; /* Rx Status       */
108         unsigned int tx    :  1; /* Tx Status       */
109         unsigned int eo    :  1; /* End of Transfer */
110         unsigned int       :  1;
111         unsigned int txffe :  1; /* Tx FIFO Empty   */
112         unsigned int txfff :  1; /* Tx FIFO Full    */
113         unsigned int rxffe :  1; /* Rx FIFO Empty   */
114         unsigned int rxfff :  1; /* Rx FIFO Full    */
115         unsigned int       : 24;
116     } bitfield;
117     u32 reg;
118 };
119
120
121
122 union kp_spi_ffctrl {
123     struct __attribute__((packed)) spi_ffctrl_bitfield {
124         unsigned int ffstart :  1; /* FIFO Start */
125         unsigned int         : 31;
126     } bitfield;
127     u32 reg;
128 };
129
130
131
132 /***************
133  * SPI Helpers *
134  ***************/
135 static inline int
136 kp_spi_bytes_per_word(int word_len)
137 {
138     if (word_len <= 8){
139         return 1;
140     }
141     else if (word_len <= 16) {
142         return 2;
143     }
144     else { /* word_len <= 32 */
145         return 4;
146     }
147 }
148
149 static inline u64
150 kp_spi_read_reg(struct kp_spi_controller_state *cs, int idx)
151 {
152     u64 __iomem *addr = cs->base;
153     u64 val;
154
155     addr += idx;
156     if ((idx == KP_SPI_REG_CONFIG) && (cs->conf_cache >= 0)){
157         return cs->conf_cache;
158     }
159     val = readq((void*)addr);
160     return val;
161 }
162
163 static inline void
164 kp_spi_write_reg(struct kp_spi_controller_state *cs, int idx, u64 val)
165 {
166     u64 __iomem *addr = cs->base;
167     addr += idx;
168     writeq(val, (void*)addr);
169     if (idx == KP_SPI_REG_CONFIG)
170         cs->conf_cache = val;
171 }
172
173 static int
174 kp_spi_wait_for_reg_bit(struct kp_spi_controller_state *cs, int idx, unsigned long bit)
175 {
176     unsigned long timeout;
177     timeout = jiffies + msecs_to_jiffies(1000);
178     while (!(kp_spi_read_reg(cs, idx) & bit)) {
179         if (time_after(jiffies, timeout)) {
180             if (!(kp_spi_read_reg(cs, idx) & bit)) {
181                 return -ETIMEDOUT;
182             } else {
183                 return 0;
184             }
185         }
186         cpu_relax();
187     }
188     return 0;
189 }
190
191 static unsigned
192 kp_spi_txrx_pio(struct spi_device *spidev, struct spi_transfer *transfer)
193 {
194     struct kp_spi_controller_state *cs = spidev->controller_state;
195     unsigned int count = transfer->len;
196     unsigned int c = count;
197     
198     int i;
199     u8 *rx       = transfer->rx_buf;
200     const u8 *tx = transfer->tx_buf;
201     int processed = 0;
202     
203     if (tx) {
204         for (i = 0 ; i < c ; i++) {
205             char val = *tx++;
206             
207             if (kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS, KP_SPI_REG_STATUS_TXS) < 0) {
208                 goto out;
209             }
210             
211             kp_spi_write_reg(cs, KP_SPI_REG_TXDATA, val);
212             processed++;
213         }
214     }
215     else if(rx) {
216         for (i = 0 ; i < c ; i++) {
217             char test=0;
218             
219             kp_spi_write_reg(cs, KP_SPI_REG_TXDATA, 0x00);
220             
221             if (kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS, KP_SPI_REG_STATUS_RXS) < 0) {
222                 goto out;
223             }
224             
225             test = kp_spi_read_reg(cs, KP_SPI_REG_RXDATA);
226             *rx++ = test;
227             processed++;
228         }
229     }
230     
231     if (kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS, KP_SPI_REG_STATUS_EOT) < 0) {
232         //TODO: Figure out how to abort transaction??  This has never happened in practice though...
233     }
234     
235  out:
236     return processed;
237 }
238
239 /*****************
240  * SPI Functions *
241  *****************/
242 static int
243 kp_spi_setup(struct spi_device *spidev)
244 {
245     union kp_spi_config sc;
246     struct kp_spi *kpspi = spi_master_get_devdata(spidev->master);
247     struct kp_spi_controller_state *cs;
248     
249     /* setup controller state */
250     cs = spidev->controller_state;
251     if (!cs) {
252         cs = kzalloc(sizeof(*cs), GFP_KERNEL);
253         if(!cs) {
254             return -ENOMEM;
255         }
256         cs->base = kpspi->base;
257         cs->phys = kpspi->phys;
258         cs->chip_select = spidev->chip_select;
259         cs->word_len = spidev->bits_per_word;
260         cs->conf_cache = -1;
261         spidev->controller_state = cs;
262     }
263     
264     /* set config register */
265     sc.bitfield.wl = spidev->bits_per_word - 1;
266     sc.bitfield.cs = spidev->chip_select;
267     sc.bitfield.spi_en = 0;
268     sc.bitfield.trm = 0;
269     sc.bitfield.ffen = 0;
270     kp_spi_write_reg(spidev->controller_state, KP_SPI_REG_CONFIG, sc.reg);
271     return 0;
272 }
273
274 static int
275 kp_spi_transfer_one_message(struct spi_master *master, struct spi_message *m)
276 {
277     struct kp_spi_controller_state *cs;
278     struct spi_device   *spidev;
279     struct kp_spi       *kpspi;
280     struct spi_transfer *transfer;
281     union kp_spi_config sc;
282     int status = 0;
283     
284     spidev = m->spi;
285     kpspi = spi_master_get_devdata(master);
286     m->actual_length = 0;
287     m->status = 0;
288     
289     cs = spidev->controller_state;
290     
291     /* reject invalid messages and transfers */
292     if (list_empty(&m->transfers)) {
293         return -EINVAL;
294     }
295     
296     /* validate input */
297     list_for_each_entry(transfer, &m->transfers, transfer_list) {
298         const void *tx_buf = transfer->tx_buf;
299         void       *rx_buf = transfer->rx_buf;
300         unsigned    len = transfer->len;
301         
302         if (transfer->speed_hz > KP_SPI_CLK || (len && !(rx_buf || tx_buf))) {
303             dev_dbg(kpspi->dev, "  transfer: %d Hz, %d %s%s, %d bpw\n",
304                     transfer->speed_hz,
305                     len,
306                     tx_buf ? "tx" : "",
307                     rx_buf ? "rx" : "",
308                     transfer->bits_per_word);
309             dev_dbg(kpspi->dev, "  transfer -EINVAL\n");
310             return -EINVAL;
311         }
312         if (transfer->speed_hz && (transfer->speed_hz < (KP_SPI_CLK >> 15))) {
313             dev_dbg(kpspi->dev, "speed_hz %d below minimum %d Hz\n",
314                     transfer->speed_hz,
315                     KP_SPI_CLK >> 15);
316             dev_dbg(kpspi->dev, "  speed_hz -EINVAL\n");
317             return -EINVAL;
318         }
319     }
320     
321     /* assert chip select to start the sequence*/
322     sc.reg = kp_spi_read_reg(cs, KP_SPI_REG_CONFIG);
323     sc.bitfield.spi_en = 1;
324     kp_spi_write_reg(cs, KP_SPI_REG_CONFIG, sc.reg);
325     
326     /* work */
327     if (kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS, KP_SPI_REG_STATUS_EOT) < 0) {
328         dev_info(kpspi->dev, "EOT timed out\n");
329         goto out;
330     }
331     
332     /* do the transfers for this message */
333     list_for_each_entry(transfer, &m->transfers, transfer_list) {
334         if (transfer->tx_buf == NULL && transfer->rx_buf == NULL && transfer->len) {
335             status = -EINVAL;
336             break;
337         }
338         
339         /* transfer */
340         if (transfer->len) {
341             unsigned int word_len = spidev->bits_per_word;
342             unsigned count;
343             
344             /* set up the transfer... */
345             sc.reg = kp_spi_read_reg(cs, KP_SPI_REG_CONFIG);
346             
347             /* ...direction */
348             if (transfer->tx_buf) {
349                 sc.bitfield.trm = KP_SPI_REG_CONFIG_TRM_TX;
350             }
351             else if (transfer->rx_buf) {
352                 sc.bitfield.trm = KP_SPI_REG_CONFIG_TRM_RX;
353             }
354             
355             /* ...word length */
356             if (transfer->bits_per_word) {
357                 word_len = transfer->bits_per_word;
358             }
359             cs->word_len = word_len;
360             sc.bitfield.wl = word_len-1;
361             
362             /* ...chip select */
363             sc.bitfield.cs = spidev->chip_select;
364             
365             /* ...and write the new settings */
366             kp_spi_write_reg(cs, KP_SPI_REG_CONFIG, sc.reg);
367             
368             /* do the transfer */
369             count = kp_spi_txrx_pio(spidev, transfer);
370             m->actual_length += count;
371             
372             if (count != transfer->len) {
373                 status = -EIO;
374                 break;
375             }
376         }
377         
378         if (transfer->delay_usecs) {
379             udelay(transfer->delay_usecs);
380         }
381     }
382     
383     /* de-assert chip select to end the sequence */
384     sc.reg = kp_spi_read_reg(cs, KP_SPI_REG_CONFIG);
385     sc.bitfield.spi_en = 0;
386     kp_spi_write_reg(cs, KP_SPI_REG_CONFIG, sc.reg);
387     
388  out:
389     /* done work */
390     spi_finalize_current_message(master);
391     return 0;
392 }
393
394 static void
395 kp_spi_cleanup(struct spi_device *spidev)
396 {
397     struct kp_spi_controller_state *cs = spidev->controller_state;
398     if (cs) {
399         kfree(cs);
400     }
401 }
402
403
404
405 /******************
406  * Probe / Remove *
407  ******************/
408 static int
409 kp_spi_probe(struct platform_device *pldev)
410 {
411     struct kpc_core_device_platdata *drvdata;
412     struct spi_master *master;
413     struct kp_spi *kpspi;
414     struct resource *r;
415     int status = 0;
416     int i;
417
418     drvdata = pldev->dev.platform_data;
419     if (!drvdata){
420         dev_err(&pldev->dev, "kp_spi_probe: platform_data is NULL!\n");
421         return -ENODEV;
422     }
423     
424     master = spi_alloc_master(&pldev->dev, sizeof(struct kp_spi));
425     if (master == NULL) {
426         dev_err(&pldev->dev, "kp_spi_probe: master allocation failed\n");
427         return -ENOMEM;
428     }
429     
430     /* set up the spi functions */
431     master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
432     master->bits_per_word_mask = (unsigned int)SPI_BPW_RANGE_MASK(4, 32);
433     master->setup = kp_spi_setup;
434     master->transfer_one_message = kp_spi_transfer_one_message;
435     master->cleanup = kp_spi_cleanup;
436     
437     platform_set_drvdata(pldev, master);
438     
439     kpspi = spi_master_get_devdata(master);
440     kpspi->master = master;
441     kpspi->dev = &pldev->dev;
442     
443     master->num_chipselect = 4;
444     if (pldev->id != -1) {
445         master->bus_num = pldev->id;
446     }
447     kpspi->pin_dir = 0;
448     
449     r = platform_get_resource(pldev, IORESOURCE_MEM, 0);
450     if (r == NULL) {
451         dev_err(&pldev->dev, "kp_spi_probe: Unable to get platform resources\n");
452         status = -ENODEV;
453         goto free_master;
454     }
455     
456     kpspi->phys = (unsigned long)ioremap_nocache(r->start, resource_size(r));
457     kpspi->base = (u64 __iomem *)kpspi->phys;
458     
459     status = spi_register_master(master);
460     if (status < 0) {
461         dev_err(&pldev->dev, "Unable to register SPI device\n");
462         goto free_master;
463     }
464     
465     /* register the slave boards */
466     #define NEW_SPI_DEVICE_FROM_BOARD_INFO_TABLE(table) \
467         for (i = 0 ; i < ARRAY_SIZE(table) ; i++) { \
468             spi_new_device(master, &(table[i])); \
469         }
470     
471     switch ((drvdata->card_id & 0xFFFF0000) >> 16){
472         case PCI_DEVICE_ID_DAKTRONICS_KADOKA_P2KR0:
473             NEW_SPI_DEVICE_FROM_BOARD_INFO_TABLE(p2kr0_board_info);
474             break;
475         default:
476             dev_err(&pldev->dev, "Unknown hardware, cant know what partition table to use!\n");
477             goto free_master;
478             break;
479     }
480     
481     return status;
482
483  free_master:
484     spi_master_put(master);
485     return status;
486 }
487
488 static int
489 kp_spi_remove(struct platform_device *pldev)
490 {
491     struct spi_master * master = platform_get_drvdata(pldev);
492     spi_unregister_master(master);
493     return 0;
494 }
495
496
497 static struct platform_driver kp_spi_driver = {
498     .driver = {
499         .name =     KP_DRIVER_NAME_SPI,
500     },
501     .probe =    kp_spi_probe,
502     .remove =   kp_spi_remove,
503 };
504
505 module_platform_driver(kp_spi_driver);
506 MODULE_LICENSE("GPL");
507 MODULE_ALIAS("platform:kp_spi");