1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OMAP7xx SPI 100k controller driver
4 * Author: Fabrice Crohas <fcrohas@gmail.com>
5 * from original omap1_mcspi driver
7 * Copyright (C) 2005, 2006 Nokia Corporation
8 * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
9 * Juha Yrj�l� <juha.yrjola@nokia.com>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.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/pm_runtime.h>
19 #include <linux/err.h>
20 #include <linux/clk.h>
22 #include <linux/gpio.h>
23 #include <linux/slab.h>
25 #include <linux/spi/spi.h>
27 #define OMAP1_SPI100K_MAX_FREQ 48000000
29 #define ICR_SPITAS (OMAP7XX_ICR_BASE + 0x12)
31 #define SPI_SETUP1 0x00
32 #define SPI_SETUP2 0x02
34 #define SPI_STATUS 0x06
35 #define SPI_TX_LSB 0x08
36 #define SPI_TX_MSB 0x0a
37 #define SPI_RX_LSB 0x0c
38 #define SPI_RX_MSB 0x0e
40 #define SPI_SETUP1_INT_READ_ENABLE (1UL << 5)
41 #define SPI_SETUP1_INT_WRITE_ENABLE (1UL << 4)
42 #define SPI_SETUP1_CLOCK_DIVISOR(x) ((x) << 1)
43 #define SPI_SETUP1_CLOCK_ENABLE (1UL << 0)
45 #define SPI_SETUP2_ACTIVE_EDGE_FALLING (0UL << 0)
46 #define SPI_SETUP2_ACTIVE_EDGE_RISING (1UL << 0)
47 #define SPI_SETUP2_NEGATIVE_LEVEL (0UL << 5)
48 #define SPI_SETUP2_POSITIVE_LEVEL (1UL << 5)
49 #define SPI_SETUP2_LEVEL_TRIGGER (0UL << 10)
50 #define SPI_SETUP2_EDGE_TRIGGER (1UL << 10)
52 #define SPI_CTRL_SEN(x) ((x) << 7)
53 #define SPI_CTRL_WORD_SIZE(x) (((x) - 1) << 2)
54 #define SPI_CTRL_WR (1UL << 1)
55 #define SPI_CTRL_RD (1UL << 0)
57 #define SPI_STATUS_WE (1UL << 1)
58 #define SPI_STATUS_RD (1UL << 0)
60 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
61 * cache operations; better heuristics consider wordsize and bitrate.
63 #define DMA_MIN_BYTES 8
66 #define SPI_SHUTDOWN 1
68 struct omap1_spi100k
{
72 /* Virtual base address of the controller */
76 struct omap1_spi100k_cs
{
81 static void spi100k_enable_clock(struct spi_master
*master
)
84 struct omap1_spi100k
*spi100k
= spi_master_get_devdata(master
);
87 val
= readw(spi100k
->base
+ SPI_SETUP1
);
88 val
|= SPI_SETUP1_CLOCK_ENABLE
;
89 writew(val
, spi100k
->base
+ SPI_SETUP1
);
92 static void spi100k_disable_clock(struct spi_master
*master
)
95 struct omap1_spi100k
*spi100k
= spi_master_get_devdata(master
);
98 val
= readw(spi100k
->base
+ SPI_SETUP1
);
99 val
&= ~SPI_SETUP1_CLOCK_ENABLE
;
100 writew(val
, spi100k
->base
+ SPI_SETUP1
);
103 static void spi100k_write_data(struct spi_master
*master
, int len
, int data
)
105 struct omap1_spi100k
*spi100k
= spi_master_get_devdata(master
);
107 /* write 16-bit word, shifting 8-bit data if necessary */
113 spi100k_enable_clock(master
);
114 writew(data
, spi100k
->base
+ SPI_TX_MSB
);
116 writew(SPI_CTRL_SEN(0) |
117 SPI_CTRL_WORD_SIZE(len
) |
119 spi100k
->base
+ SPI_CTRL
);
121 /* Wait for bit ack send change */
122 while ((readw(spi100k
->base
+ SPI_STATUS
) & SPI_STATUS_WE
) != SPI_STATUS_WE
)
126 spi100k_disable_clock(master
);
129 static int spi100k_read_data(struct spi_master
*master
, int len
)
132 struct omap1_spi100k
*spi100k
= spi_master_get_devdata(master
);
134 /* Always do at least 16 bits */
138 spi100k_enable_clock(master
);
139 writew(SPI_CTRL_SEN(0) |
140 SPI_CTRL_WORD_SIZE(len
) |
142 spi100k
->base
+ SPI_CTRL
);
144 while ((readw(spi100k
->base
+ SPI_STATUS
) & SPI_STATUS_RD
) != SPI_STATUS_RD
)
148 dataL
= readw(spi100k
->base
+ SPI_RX_LSB
);
149 readw(spi100k
->base
+ SPI_RX_MSB
);
150 spi100k_disable_clock(master
);
155 static void spi100k_open(struct spi_master
*master
)
157 /* get control of SPI */
158 struct omap1_spi100k
*spi100k
= spi_master_get_devdata(master
);
160 writew(SPI_SETUP1_INT_READ_ENABLE
|
161 SPI_SETUP1_INT_WRITE_ENABLE
|
162 SPI_SETUP1_CLOCK_DIVISOR(0), spi100k
->base
+ SPI_SETUP1
);
164 /* configure clock and interrupts */
165 writew(SPI_SETUP2_ACTIVE_EDGE_FALLING
|
166 SPI_SETUP2_NEGATIVE_LEVEL
|
167 SPI_SETUP2_LEVEL_TRIGGER
, spi100k
->base
+ SPI_SETUP2
);
170 static void omap1_spi100k_force_cs(struct omap1_spi100k
*spi100k
, int enable
)
173 writew(0x05fc, spi100k
->base
+ SPI_CTRL
);
175 writew(0x05fd, spi100k
->base
+ SPI_CTRL
);
179 omap1_spi100k_txrx_pio(struct spi_device
*spi
, struct spi_transfer
*xfer
)
181 struct omap1_spi100k_cs
*cs
= spi
->controller_state
;
182 unsigned int count
, c
;
187 word_len
= cs
->word_len
;
197 if (xfer
->tx_buf
!= NULL
)
198 spi100k_write_data(spi
->master
, word_len
, *tx
++);
199 if (xfer
->rx_buf
!= NULL
)
200 *rx
++ = spi100k_read_data(spi
->master
, word_len
);
202 } else if (word_len
<= 16) {
210 if (xfer
->tx_buf
!= NULL
)
211 spi100k_write_data(spi
->master
, word_len
, *tx
++);
212 if (xfer
->rx_buf
!= NULL
)
213 *rx
++ = spi100k_read_data(spi
->master
, word_len
);
215 } else if (word_len
<= 32) {
223 if (xfer
->tx_buf
!= NULL
)
224 spi100k_write_data(spi
->master
, word_len
, *tx
);
225 if (xfer
->rx_buf
!= NULL
)
226 *rx
= spi100k_read_data(spi
->master
, word_len
);
232 /* called only when no transfer is active to this device */
233 static int omap1_spi100k_setup_transfer(struct spi_device
*spi
,
234 struct spi_transfer
*t
)
236 struct omap1_spi100k
*spi100k
= spi_master_get_devdata(spi
->master
);
237 struct omap1_spi100k_cs
*cs
= spi
->controller_state
;
241 word_len
= t
->bits_per_word
;
243 word_len
= spi
->bits_per_word
;
245 if (spi
->bits_per_word
> 32)
247 cs
->word_len
= word_len
;
249 /* SPI init before transfer */
250 writew(0x3e , spi100k
->base
+ SPI_SETUP1
);
251 writew(0x00 , spi100k
->base
+ SPI_STATUS
);
252 writew(0x3e , spi100k
->base
+ SPI_CTRL
);
257 /* the spi->mode bits understood by this driver: */
258 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
260 static int omap1_spi100k_setup(struct spi_device
*spi
)
263 struct omap1_spi100k
*spi100k
;
264 struct omap1_spi100k_cs
*cs
= spi
->controller_state
;
266 spi100k
= spi_master_get_devdata(spi
->master
);
269 cs
= devm_kzalloc(&spi
->dev
, sizeof(*cs
), GFP_KERNEL
);
272 cs
->base
= spi100k
->base
+ spi
->chip_select
* 0x14;
273 spi
->controller_state
= cs
;
276 spi100k_open(spi
->master
);
278 clk_prepare_enable(spi100k
->ick
);
279 clk_prepare_enable(spi100k
->fck
);
281 ret
= omap1_spi100k_setup_transfer(spi
, NULL
);
283 clk_disable_unprepare(spi100k
->ick
);
284 clk_disable_unprepare(spi100k
->fck
);
289 static int omap1_spi100k_transfer_one_message(struct spi_master
*master
,
290 struct spi_message
*m
)
292 struct omap1_spi100k
*spi100k
= spi_master_get_devdata(master
);
293 struct spi_device
*spi
= m
->spi
;
294 struct spi_transfer
*t
= NULL
;
298 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
299 if (t
->tx_buf
== NULL
&& t
->rx_buf
== NULL
&& t
->len
) {
303 status
= omap1_spi100k_setup_transfer(spi
, t
);
308 omap1_spi100k_force_cs(spi100k
, 1);
315 count
= omap1_spi100k_txrx_pio(spi
, t
);
316 m
->actual_length
+= count
;
318 if (count
!= t
->len
) {
324 spi_transfer_delay_exec(t
);
326 /* ignore the "leave it on after last xfer" hint */
329 omap1_spi100k_force_cs(spi100k
, 0);
334 status
= omap1_spi100k_setup_transfer(spi
, NULL
);
337 omap1_spi100k_force_cs(spi100k
, 0);
341 spi_finalize_current_message(master
);
346 static int omap1_spi100k_probe(struct platform_device
*pdev
)
348 struct spi_master
*master
;
349 struct omap1_spi100k
*spi100k
;
355 master
= spi_alloc_master(&pdev
->dev
, sizeof(*spi100k
));
356 if (master
== NULL
) {
357 dev_dbg(&pdev
->dev
, "master allocation failed\n");
362 master
->bus_num
= pdev
->id
;
364 master
->setup
= omap1_spi100k_setup
;
365 master
->transfer_one_message
= omap1_spi100k_transfer_one_message
;
366 master
->num_chipselect
= 2;
367 master
->mode_bits
= MODEBITS
;
368 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
369 master
->min_speed_hz
= OMAP1_SPI100K_MAX_FREQ
/(1<<16);
370 master
->max_speed_hz
= OMAP1_SPI100K_MAX_FREQ
;
371 master
->auto_runtime_pm
= true;
373 spi100k
= spi_master_get_devdata(master
);
376 * The memory region base address is taken as the platform_data.
377 * You should allocate this with ioremap() before initializing
380 spi100k
->base
= (void __iomem
*)dev_get_platdata(&pdev
->dev
);
382 spi100k
->ick
= devm_clk_get(&pdev
->dev
, "ick");
383 if (IS_ERR(spi100k
->ick
)) {
384 dev_dbg(&pdev
->dev
, "can't get spi100k_ick\n");
385 status
= PTR_ERR(spi100k
->ick
);
389 spi100k
->fck
= devm_clk_get(&pdev
->dev
, "fck");
390 if (IS_ERR(spi100k
->fck
)) {
391 dev_dbg(&pdev
->dev
, "can't get spi100k_fck\n");
392 status
= PTR_ERR(spi100k
->fck
);
396 status
= clk_prepare_enable(spi100k
->ick
);
398 dev_err(&pdev
->dev
, "failed to enable ick: %d\n", status
);
402 status
= clk_prepare_enable(spi100k
->fck
);
404 dev_err(&pdev
->dev
, "failed to enable fck: %d\n", status
);
408 pm_runtime_enable(&pdev
->dev
);
409 pm_runtime_set_active(&pdev
->dev
);
411 status
= devm_spi_register_master(&pdev
->dev
, master
);
418 clk_disable_unprepare(spi100k
->fck
);
420 clk_disable_unprepare(spi100k
->ick
);
422 spi_master_put(master
);
426 static int omap1_spi100k_remove(struct platform_device
*pdev
)
428 struct spi_master
*master
= spi_master_get(platform_get_drvdata(pdev
));
429 struct omap1_spi100k
*spi100k
= spi_master_get_devdata(master
);
431 pm_runtime_disable(&pdev
->dev
);
433 clk_disable_unprepare(spi100k
->fck
);
434 clk_disable_unprepare(spi100k
->ick
);
440 static int omap1_spi100k_runtime_suspend(struct device
*dev
)
442 struct spi_master
*master
= spi_master_get(dev_get_drvdata(dev
));
443 struct omap1_spi100k
*spi100k
= spi_master_get_devdata(master
);
445 clk_disable_unprepare(spi100k
->ick
);
446 clk_disable_unprepare(spi100k
->fck
);
451 static int omap1_spi100k_runtime_resume(struct device
*dev
)
453 struct spi_master
*master
= spi_master_get(dev_get_drvdata(dev
));
454 struct omap1_spi100k
*spi100k
= spi_master_get_devdata(master
);
457 ret
= clk_prepare_enable(spi100k
->ick
);
459 dev_err(dev
, "Failed to enable ick: %d\n", ret
);
463 ret
= clk_prepare_enable(spi100k
->fck
);
465 dev_err(dev
, "Failed to enable fck: %d\n", ret
);
466 clk_disable_unprepare(spi100k
->ick
);
474 static const struct dev_pm_ops omap1_spi100k_pm
= {
475 SET_RUNTIME_PM_OPS(omap1_spi100k_runtime_suspend
,
476 omap1_spi100k_runtime_resume
, NULL
)
479 static struct platform_driver omap1_spi100k_driver
= {
481 .name
= "omap1_spi100k",
482 .pm
= &omap1_spi100k_pm
,
484 .probe
= omap1_spi100k_probe
,
485 .remove
= omap1_spi100k_remove
,
488 module_platform_driver(omap1_spi100k_driver
);
490 MODULE_DESCRIPTION("OMAP7xx SPI 100k controller driver");
491 MODULE_AUTHOR("Fabrice Crohas <fcrohas@gmail.com>");
492 MODULE_LICENSE("GPL");