blob: 0b07c746453dbe6cebfd92cc14770738cda8cce1 [file] [log] [blame]
Radu Pireae1892542018-07-13 19:47:35 +03001// SPDX-License-Identifier: GPL-2.0
2//
3// Driver for AT91 USART Controllers as SPI
4//
5// Copyright (C) 2018 Microchip Technology Inc.
6//
7// Author: Radu Pirea <radu.pirea@microchip.com>
8
9#include <linux/clk.h>
10#include <linux/delay.h>
11#include <linux/interrupt.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/of_gpio.h>
15#include <linux/platform_device.h>
Radu Pirea96ed3ec2018-11-21 13:27:30 +020016#include <linux/pm_runtime.h>
Radu Pireae1892542018-07-13 19:47:35 +030017
18#include <linux/spi/spi.h>
19
20#define US_CR 0x00
21#define US_MR 0x04
22#define US_IER 0x08
23#define US_IDR 0x0C
24#define US_CSR 0x14
25#define US_RHR 0x18
26#define US_THR 0x1C
27#define US_BRGR 0x20
28#define US_VERSION 0xFC
29
30#define US_CR_RSTRX BIT(2)
31#define US_CR_RSTTX BIT(3)
32#define US_CR_RXEN BIT(4)
33#define US_CR_RXDIS BIT(5)
34#define US_CR_TXEN BIT(6)
35#define US_CR_TXDIS BIT(7)
36
37#define US_MR_SPI_MASTER 0x0E
38#define US_MR_CHRL GENMASK(7, 6)
39#define US_MR_CPHA BIT(8)
40#define US_MR_CPOL BIT(16)
41#define US_MR_CLKO BIT(18)
42#define US_MR_WRDBT BIT(20)
43#define US_MR_LOOP BIT(15)
44
45#define US_IR_RXRDY BIT(0)
46#define US_IR_TXRDY BIT(1)
47#define US_IR_OVRE BIT(5)
48
49#define US_BRGR_SIZE BIT(16)
50
51#define US_MIN_CLK_DIV 0x06
52#define US_MAX_CLK_DIV BIT(16)
53
54#define US_RESET (US_CR_RSTRX | US_CR_RSTTX)
55#define US_DISABLE (US_CR_RXDIS | US_CR_TXDIS)
56#define US_ENABLE (US_CR_RXEN | US_CR_TXEN)
57#define US_OVRE_RXRDY_IRQS (US_IR_OVRE | US_IR_RXRDY)
58
59#define US_INIT \
60 (US_MR_SPI_MASTER | US_MR_CHRL | US_MR_CLKO | US_MR_WRDBT)
61
62/* Register access macros */
63#define at91_usart_spi_readl(port, reg) \
64 readl_relaxed((port)->regs + US_##reg)
65#define at91_usart_spi_writel(port, reg, value) \
66 writel_relaxed((value), (port)->regs + US_##reg)
67
68#define at91_usart_spi_readb(port, reg) \
69 readb_relaxed((port)->regs + US_##reg)
70#define at91_usart_spi_writeb(port, reg, value) \
71 writeb_relaxed((value), (port)->regs + US_##reg)
72
73struct at91_usart_spi {
74 struct spi_transfer *current_transfer;
75 void __iomem *regs;
76 struct device *dev;
77 struct clk *clk;
78
79 /*used in interrupt to protect data reading*/
80 spinlock_t lock;
81
82 int irq;
83 unsigned int current_tx_remaining_bytes;
84 unsigned int current_rx_remaining_bytes;
85
86 u32 spi_clk;
87 u32 status;
88
89 bool xfer_failed;
90};
91
92static inline u32 at91_usart_spi_tx_ready(struct at91_usart_spi *aus)
93{
94 return aus->status & US_IR_TXRDY;
95}
96
97static inline u32 at91_usart_spi_rx_ready(struct at91_usart_spi *aus)
98{
99 return aus->status & US_IR_RXRDY;
100}
101
102static inline u32 at91_usart_spi_check_overrun(struct at91_usart_spi *aus)
103{
104 return aus->status & US_IR_OVRE;
105}
106
107static inline u32 at91_usart_spi_read_status(struct at91_usart_spi *aus)
108{
109 aus->status = at91_usart_spi_readl(aus, CSR);
110 return aus->status;
111}
112
113static inline void at91_usart_spi_tx(struct at91_usart_spi *aus)
114{
115 unsigned int len = aus->current_transfer->len;
116 unsigned int remaining = aus->current_tx_remaining_bytes;
117 const u8 *tx_buf = aus->current_transfer->tx_buf;
118
119 if (!remaining)
120 return;
121
122 if (at91_usart_spi_tx_ready(aus)) {
123 at91_usart_spi_writeb(aus, THR, tx_buf[len - remaining]);
124 aus->current_tx_remaining_bytes--;
125 }
126}
127
128static inline void at91_usart_spi_rx(struct at91_usart_spi *aus)
129{
130 int len = aus->current_transfer->len;
131 int remaining = aus->current_rx_remaining_bytes;
132 u8 *rx_buf = aus->current_transfer->rx_buf;
133
134 if (!remaining)
135 return;
136
137 rx_buf[len - remaining] = at91_usart_spi_readb(aus, RHR);
138 aus->current_rx_remaining_bytes--;
139}
140
141static inline void
142at91_usart_spi_set_xfer_speed(struct at91_usart_spi *aus,
143 struct spi_transfer *xfer)
144{
145 at91_usart_spi_writel(aus, BRGR,
146 DIV_ROUND_UP(aus->spi_clk, xfer->speed_hz));
147}
148
149static irqreturn_t at91_usart_spi_interrupt(int irq, void *dev_id)
150{
151 struct spi_controller *controller = dev_id;
152 struct at91_usart_spi *aus = spi_master_get_devdata(controller);
153
154 spin_lock(&aus->lock);
155 at91_usart_spi_read_status(aus);
156
157 if (at91_usart_spi_check_overrun(aus)) {
158 aus->xfer_failed = true;
159 at91_usart_spi_writel(aus, IDR, US_IR_OVRE | US_IR_RXRDY);
160 spin_unlock(&aus->lock);
161 return IRQ_HANDLED;
162 }
163
164 if (at91_usart_spi_rx_ready(aus)) {
165 at91_usart_spi_rx(aus);
166 spin_unlock(&aus->lock);
167 return IRQ_HANDLED;
168 }
169
170 spin_unlock(&aus->lock);
171
172 return IRQ_NONE;
173}
174
175static int at91_usart_spi_setup(struct spi_device *spi)
176{
177 struct at91_usart_spi *aus = spi_master_get_devdata(spi->controller);
178 u32 *ausd = spi->controller_state;
179 unsigned int mr = at91_usart_spi_readl(aus, MR);
180 u8 bits = spi->bits_per_word;
181
182 if (bits != 8) {
183 dev_dbg(&spi->dev, "Only 8 bits per word are supported\n");
184 return -EINVAL;
185 }
186
187 if (spi->mode & SPI_CPOL)
188 mr |= US_MR_CPOL;
189 else
190 mr &= ~US_MR_CPOL;
191
192 if (spi->mode & SPI_CPHA)
193 mr |= US_MR_CPHA;
194 else
195 mr &= ~US_MR_CPHA;
196
197 if (spi->mode & SPI_LOOP)
198 mr |= US_MR_LOOP;
199 else
200 mr &= ~US_MR_LOOP;
201
202 if (!ausd) {
203 ausd = kzalloc(sizeof(*ausd), GFP_KERNEL);
204 if (!ausd)
205 return -ENOMEM;
206
207 spi->controller_state = ausd;
208 }
209
210 *ausd = mr;
211
212 dev_dbg(&spi->dev,
213 "setup: bpw %u mode 0x%x -> mr %d %08x\n",
214 bits, spi->mode, spi->chip_select, mr);
215
216 return 0;
217}
218
Lee Jones259e0a02018-09-11 11:38:50 +0100219static int at91_usart_spi_transfer_one(struct spi_controller *ctlr,
220 struct spi_device *spi,
221 struct spi_transfer *xfer)
Radu Pireae1892542018-07-13 19:47:35 +0300222{
223 struct at91_usart_spi *aus = spi_master_get_devdata(ctlr);
224
225 at91_usart_spi_set_xfer_speed(aus, xfer);
226 aus->xfer_failed = false;
227 aus->current_transfer = xfer;
228 aus->current_tx_remaining_bytes = xfer->len;
229 aus->current_rx_remaining_bytes = xfer->len;
230
231 while ((aus->current_tx_remaining_bytes ||
232 aus->current_rx_remaining_bytes) && !aus->xfer_failed) {
233 at91_usart_spi_read_status(aus);
234 at91_usart_spi_tx(aus);
235 cpu_relax();
236 }
237
238 if (aus->xfer_failed) {
239 dev_err(aus->dev, "Overrun!\n");
240 return -EIO;
241 }
242
243 return 0;
244}
245
Lee Jones259e0a02018-09-11 11:38:50 +0100246static int at91_usart_spi_prepare_message(struct spi_controller *ctlr,
247 struct spi_message *message)
Radu Pireae1892542018-07-13 19:47:35 +0300248{
249 struct at91_usart_spi *aus = spi_master_get_devdata(ctlr);
250 struct spi_device *spi = message->spi;
251 u32 *ausd = spi->controller_state;
252
253 at91_usart_spi_writel(aus, CR, US_ENABLE);
254 at91_usart_spi_writel(aus, IER, US_OVRE_RXRDY_IRQS);
255 at91_usart_spi_writel(aus, MR, *ausd);
256
257 return 0;
258}
259
Lee Jones259e0a02018-09-11 11:38:50 +0100260static int at91_usart_spi_unprepare_message(struct spi_controller *ctlr,
261 struct spi_message *message)
Radu Pireae1892542018-07-13 19:47:35 +0300262{
263 struct at91_usart_spi *aus = spi_master_get_devdata(ctlr);
264
265 at91_usart_spi_writel(aus, CR, US_RESET | US_DISABLE);
266 at91_usart_spi_writel(aus, IDR, US_OVRE_RXRDY_IRQS);
267
268 return 0;
269}
270
271static void at91_usart_spi_cleanup(struct spi_device *spi)
272{
273 struct at91_usart_spi_device *ausd = spi->controller_state;
274
275 spi->controller_state = NULL;
276 kfree(ausd);
277}
278
279static void at91_usart_spi_init(struct at91_usart_spi *aus)
280{
281 at91_usart_spi_writel(aus, MR, US_INIT);
282 at91_usart_spi_writel(aus, CR, US_RESET | US_DISABLE);
283}
284
285static int at91_usart_gpio_setup(struct platform_device *pdev)
286{
287 struct device_node *np = pdev->dev.parent->of_node;
288 int i;
289 int ret;
290 int nb;
291
292 if (!np)
293 return -EINVAL;
294
295 nb = of_gpio_named_count(np, "cs-gpios");
296 for (i = 0; i < nb; i++) {
297 int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);
298
299 if (cs_gpio < 0)
300 return cs_gpio;
301
302 if (gpio_is_valid(cs_gpio)) {
303 ret = devm_gpio_request_one(&pdev->dev, cs_gpio,
304 GPIOF_DIR_OUT,
305 dev_name(&pdev->dev));
306 if (ret)
307 return ret;
308 }
309 }
310
311 return 0;
312}
313
314static int at91_usart_spi_probe(struct platform_device *pdev)
315{
316 struct resource *regs;
317 struct spi_controller *controller;
318 struct at91_usart_spi *aus;
319 struct clk *clk;
320 int irq;
321 int ret;
322
323 regs = platform_get_resource(to_platform_device(pdev->dev.parent),
324 IORESOURCE_MEM, 0);
325 if (!regs)
326 return -EINVAL;
327
328 irq = platform_get_irq(to_platform_device(pdev->dev.parent), 0);
329 if (irq < 0)
330 return irq;
331
332 clk = devm_clk_get(pdev->dev.parent, "usart");
333 if (IS_ERR(clk))
334 return PTR_ERR(clk);
335
336 ret = -ENOMEM;
337 controller = spi_alloc_master(&pdev->dev, sizeof(*aus));
338 if (!controller)
339 goto at91_usart_spi_probe_fail;
340
341 ret = at91_usart_gpio_setup(pdev);
342 if (ret)
343 goto at91_usart_spi_probe_fail;
344
345 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH;
346 controller->dev.of_node = pdev->dev.parent->of_node;
347 controller->bits_per_word_mask = SPI_BPW_MASK(8);
348 controller->setup = at91_usart_spi_setup;
349 controller->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
350 controller->transfer_one = at91_usart_spi_transfer_one;
351 controller->prepare_message = at91_usart_spi_prepare_message;
352 controller->unprepare_message = at91_usart_spi_unprepare_message;
353 controller->cleanup = at91_usart_spi_cleanup;
354 controller->max_speed_hz = DIV_ROUND_UP(clk_get_rate(clk),
355 US_MIN_CLK_DIV);
356 controller->min_speed_hz = DIV_ROUND_UP(clk_get_rate(clk),
357 US_MAX_CLK_DIV);
358 platform_set_drvdata(pdev, controller);
359
360 aus = spi_master_get_devdata(controller);
361
362 aus->dev = &pdev->dev;
363 aus->regs = devm_ioremap_resource(&pdev->dev, regs);
364 if (IS_ERR(aus->regs)) {
365 ret = PTR_ERR(aus->regs);
366 goto at91_usart_spi_probe_fail;
367 }
368
369 aus->irq = irq;
370 aus->clk = clk;
371
372 ret = devm_request_irq(&pdev->dev, irq, at91_usart_spi_interrupt, 0,
373 dev_name(&pdev->dev), controller);
374 if (ret)
375 goto at91_usart_spi_probe_fail;
376
377 ret = clk_prepare_enable(clk);
378 if (ret)
379 goto at91_usart_spi_probe_fail;
380
381 aus->spi_clk = clk_get_rate(clk);
382 at91_usart_spi_init(aus);
383
384 spin_lock_init(&aus->lock);
385 ret = devm_spi_register_master(&pdev->dev, controller);
386 if (ret)
387 goto at91_usart_fail_register_master;
388
389 dev_info(&pdev->dev,
390 "AT91 USART SPI Controller version 0x%x at %pa (irq %d)\n",
391 at91_usart_spi_readl(aus, VERSION),
392 &regs->start, irq);
393
394 return 0;
395
396at91_usart_fail_register_master:
397 clk_disable_unprepare(clk);
398at91_usart_spi_probe_fail:
399 spi_master_put(controller);
400 return ret;
401}
402
Radu Pirea96ed3ec2018-11-21 13:27:30 +0200403__maybe_unused static int at91_usart_spi_runtime_suspend(struct device *dev)
404{
405 struct spi_controller *ctlr = dev_get_drvdata(dev);
406 struct at91_usart_spi *aus = spi_master_get_devdata(ctlr);
407
408 clk_disable_unprepare(aus->clk);
409 pinctrl_pm_select_sleep_state(dev);
410
411 return 0;
412}
413
414__maybe_unused static int at91_usart_spi_runtime_resume(struct device *dev)
415{
416 struct spi_controller *ctrl = dev_get_drvdata(dev);
417 struct at91_usart_spi *aus = spi_master_get_devdata(ctrl);
418
419 pinctrl_pm_select_default_state(dev);
420
421 return clk_prepare_enable(aus->clk);
422}
423
424__maybe_unused static int at91_usart_spi_suspend(struct device *dev)
425{
426 struct spi_controller *ctrl = dev_get_drvdata(dev);
427 int ret;
428
429 ret = spi_controller_suspend(ctrl);
430 if (ret)
431 return ret;
432
433 if (!pm_runtime_suspended(dev))
434 at91_usart_spi_runtime_suspend(dev);
435
436 return 0;
437}
438
439__maybe_unused static int at91_usart_spi_resume(struct device *dev)
440{
441 struct spi_controller *ctrl = dev_get_drvdata(dev);
442 struct at91_usart_spi *aus = spi_master_get_devdata(ctrl);
443 int ret;
444
445 if (!pm_runtime_suspended(dev)) {
446 ret = at91_usart_spi_runtime_resume(dev);
447 if (ret)
448 return ret;
449 }
450
451 at91_usart_spi_init(aus);
452
453 return spi_controller_resume(ctrl);
454}
455
Radu Pireae1892542018-07-13 19:47:35 +0300456static int at91_usart_spi_remove(struct platform_device *pdev)
457{
458 struct spi_controller *ctlr = platform_get_drvdata(pdev);
459 struct at91_usart_spi *aus = spi_master_get_devdata(ctlr);
460
461 clk_disable_unprepare(aus->clk);
462
463 return 0;
464}
465
Radu Pirea96ed3ec2018-11-21 13:27:30 +0200466static const struct dev_pm_ops at91_usart_spi_pm_ops = {
467 SET_SYSTEM_SLEEP_PM_OPS(at91_usart_spi_suspend, at91_usart_spi_resume)
468 SET_RUNTIME_PM_OPS(at91_usart_spi_runtime_suspend,
469 at91_usart_spi_runtime_resume, NULL)
470};
471
Radu Pireae1892542018-07-13 19:47:35 +0300472static const struct of_device_id at91_usart_spi_dt_ids[] = {
473 { .compatible = "microchip,at91sam9g45-usart-spi"},
474 { /* sentinel */}
475};
476
477MODULE_DEVICE_TABLE(of, at91_usart_spi_dt_ids);
478
479static struct platform_driver at91_usart_spi_driver = {
480 .driver = {
481 .name = "at91_usart_spi",
Radu Pirea96ed3ec2018-11-21 13:27:30 +0200482 .pm = &at91_usart_spi_pm_ops,
Radu Pireae1892542018-07-13 19:47:35 +0300483 },
484 .probe = at91_usart_spi_probe,
485 .remove = at91_usart_spi_remove,
486};
487
488module_platform_driver(at91_usart_spi_driver);
489
490MODULE_DESCRIPTION("Microchip AT91 USART SPI Controller driver");
491MODULE_AUTHOR("Radu Pirea <radu.pirea@microchip.com>");
492MODULE_LICENSE("GPL v2");
493MODULE_ALIAS("platform:at91_usart_spi");