Wolfram Sang | 9135bac | 2018-08-22 00:02:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 2 | /* |
| 3 | * SuperH HSPI bus driver |
| 4 | * |
| 5 | * Copyright (C) 2011 Kuninori Morimoto |
| 6 | * |
| 7 | * Based on spi-sh.c: |
| 8 | * Based on pxa2xx_spi.c: |
| 9 | * Copyright (C) 2011 Renesas Solutions Corp. |
| 10 | * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 11 | */ |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 12 | |
| 13 | #include <linux/clk.h> |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/timer.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/list.h> |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/pm_runtime.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/spi/spi.h> |
| 24 | #include <linux/spi/sh_hspi.h> |
| 25 | |
| 26 | #define SPCR 0x00 |
| 27 | #define SPSR 0x04 |
| 28 | #define SPSCR 0x08 |
| 29 | #define SPTBR 0x0C |
| 30 | #define SPRBR 0x10 |
| 31 | #define SPCR2 0x14 |
| 32 | |
| 33 | /* SPSR */ |
| 34 | #define RXFL (1 << 2) |
| 35 | |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 36 | struct hspi_priv { |
| 37 | void __iomem *addr; |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 38 | struct spi_controller *ctlr; |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 39 | struct device *dev; |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 40 | struct clk *clk; |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * basic function |
| 45 | */ |
| 46 | static void hspi_write(struct hspi_priv *hspi, int reg, u32 val) |
| 47 | { |
| 48 | iowrite32(val, hspi->addr + reg); |
| 49 | } |
| 50 | |
| 51 | static u32 hspi_read(struct hspi_priv *hspi, int reg) |
| 52 | { |
| 53 | return ioread32(hspi->addr + reg); |
| 54 | } |
| 55 | |
Phil Edworthy | ce32930 | 2012-11-22 14:37:27 +0000 | [diff] [blame] | 56 | static void hspi_bit_set(struct hspi_priv *hspi, int reg, u32 mask, u32 set) |
| 57 | { |
| 58 | u32 val = hspi_read(hspi, reg); |
| 59 | |
| 60 | val &= ~mask; |
| 61 | val |= set & mask; |
| 62 | |
| 63 | hspi_write(hspi, reg, val); |
| 64 | } |
| 65 | |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 66 | /* |
| 67 | * transfer function |
| 68 | */ |
| 69 | static int hspi_status_check_timeout(struct hspi_priv *hspi, u32 mask, u32 val) |
| 70 | { |
| 71 | int t = 256; |
| 72 | |
| 73 | while (t--) { |
| 74 | if ((mask & hspi_read(hspi, SPSR)) == val) |
| 75 | return 0; |
| 76 | |
Kuninori Morimoto | bc2bfff | 2013-05-26 17:59:20 -0700 | [diff] [blame] | 77 | udelay(10); |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | dev_err(hspi->dev, "timeout\n"); |
| 81 | return -ETIMEDOUT; |
| 82 | } |
| 83 | |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 84 | /* |
| 85 | * spi master function |
| 86 | */ |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 87 | |
Phil Edworthy | ce32930 | 2012-11-22 14:37:27 +0000 | [diff] [blame] | 88 | #define hspi_hw_cs_enable(hspi) hspi_hw_cs_ctrl(hspi, 0) |
| 89 | #define hspi_hw_cs_disable(hspi) hspi_hw_cs_ctrl(hspi, 1) |
| 90 | static void hspi_hw_cs_ctrl(struct hspi_priv *hspi, int hi) |
| 91 | { |
| 92 | hspi_bit_set(hspi, SPSCR, (1 << 6), (hi) << 6); |
| 93 | } |
| 94 | |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 95 | static void hspi_hw_setup(struct hspi_priv *hspi, |
| 96 | struct spi_message *msg, |
| 97 | struct spi_transfer *t) |
| 98 | { |
| 99 | struct spi_device *spi = msg->spi; |
| 100 | struct device *dev = hspi->dev; |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 101 | u32 spcr, idiv_clk; |
| 102 | u32 rate, best_rate, min, tmp; |
| 103 | |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 104 | /* |
| 105 | * find best IDIV/CLKCx settings |
| 106 | */ |
| 107 | min = ~0; |
| 108 | best_rate = 0; |
| 109 | spcr = 0; |
| 110 | for (idiv_clk = 0x00; idiv_clk <= 0x3F; idiv_clk++) { |
| 111 | rate = clk_get_rate(hspi->clk); |
| 112 | |
| 113 | /* IDIV calculation */ |
| 114 | if (idiv_clk & (1 << 5)) |
| 115 | rate /= 128; |
| 116 | else |
| 117 | rate /= 16; |
| 118 | |
| 119 | /* CLKCx calculation */ |
Jingoo Han | a29c8ae | 2013-10-14 10:35:42 +0900 | [diff] [blame] | 120 | rate /= (((idiv_clk & 0x1F) + 1) * 2); |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 121 | |
| 122 | /* save best settings */ |
Axel Lin | e428a42 | 2014-03-02 23:01:50 +0800 | [diff] [blame] | 123 | tmp = abs(t->speed_hz - rate); |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 124 | if (tmp < min) { |
| 125 | min = tmp; |
| 126 | spcr = idiv_clk; |
| 127 | best_rate = rate; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (spi->mode & SPI_CPHA) |
| 132 | spcr |= 1 << 7; |
| 133 | if (spi->mode & SPI_CPOL) |
| 134 | spcr |= 1 << 6; |
| 135 | |
Axel Lin | e428a42 | 2014-03-02 23:01:50 +0800 | [diff] [blame] | 136 | dev_dbg(dev, "speed %d/%d\n", t->speed_hz, best_rate); |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 137 | |
| 138 | hspi_write(hspi, SPCR, spcr); |
| 139 | hspi_write(hspi, SPSR, 0x0); |
Phil Edworthy | ce32930 | 2012-11-22 14:37:27 +0000 | [diff] [blame] | 140 | hspi_write(hspi, SPSCR, 0x21); /* master mode / CS control */ |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 143 | static int hspi_transfer_one_message(struct spi_controller *ctlr, |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 144 | struct spi_message *msg) |
| 145 | { |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 146 | struct hspi_priv *hspi = spi_controller_get_devdata(ctlr); |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 147 | struct spi_transfer *t; |
Kuninori Morimoto | bb9c568 | 2012-03-14 02:48:25 -0700 | [diff] [blame] | 148 | u32 tx; |
| 149 | u32 rx; |
| 150 | int ret, i; |
Phil Edworthy | ce32930 | 2012-11-22 14:37:27 +0000 | [diff] [blame] | 151 | unsigned int cs_change; |
| 152 | const int nsecs = 50; |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 153 | |
| 154 | dev_dbg(hspi->dev, "%s\n", __func__); |
| 155 | |
Phil Edworthy | ce32930 | 2012-11-22 14:37:27 +0000 | [diff] [blame] | 156 | cs_change = 1; |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 157 | ret = 0; |
| 158 | list_for_each_entry(t, &msg->transfers, transfer_list) { |
Phil Edworthy | ce32930 | 2012-11-22 14:37:27 +0000 | [diff] [blame] | 159 | |
| 160 | if (cs_change) { |
| 161 | hspi_hw_setup(hspi, msg, t); |
| 162 | hspi_hw_cs_enable(hspi); |
| 163 | ndelay(nsecs); |
| 164 | } |
| 165 | cs_change = t->cs_change; |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 166 | |
Kuninori Morimoto | bb9c568 | 2012-03-14 02:48:25 -0700 | [diff] [blame] | 167 | for (i = 0; i < t->len; i++) { |
| 168 | |
| 169 | /* wait remains */ |
| 170 | ret = hspi_status_check_timeout(hspi, 0x1, 0); |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 171 | if (ret < 0) |
Kuninori Morimoto | bb9c568 | 2012-03-14 02:48:25 -0700 | [diff] [blame] | 172 | break; |
| 173 | |
| 174 | tx = 0; |
| 175 | if (t->tx_buf) |
| 176 | tx = (u32)((u8 *)t->tx_buf)[i]; |
| 177 | |
| 178 | hspi_write(hspi, SPTBR, tx); |
| 179 | |
Geert Uytterhoeven | c6c07b4 | 2014-01-12 14:03:38 +0100 | [diff] [blame] | 180 | /* wait receive */ |
Kuninori Morimoto | bb9c568 | 2012-03-14 02:48:25 -0700 | [diff] [blame] | 181 | ret = hspi_status_check_timeout(hspi, 0x4, 0x4); |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 182 | if (ret < 0) |
Kuninori Morimoto | bb9c568 | 2012-03-14 02:48:25 -0700 | [diff] [blame] | 183 | break; |
| 184 | |
| 185 | rx = hspi_read(hspi, SPRBR); |
| 186 | if (t->rx_buf) |
| 187 | ((u8 *)t->rx_buf)[i] = (u8)rx; |
| 188 | |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 189 | } |
Kuninori Morimoto | bb9c568 | 2012-03-14 02:48:25 -0700 | [diff] [blame] | 190 | |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 191 | msg->actual_length += t->len; |
Phil Edworthy | ce32930 | 2012-11-22 14:37:27 +0000 | [diff] [blame] | 192 | |
| 193 | if (t->delay_usecs) |
| 194 | udelay(t->delay_usecs); |
| 195 | |
| 196 | if (cs_change) { |
| 197 | ndelay(nsecs); |
| 198 | hspi_hw_cs_disable(hspi); |
| 199 | ndelay(nsecs); |
| 200 | } |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 201 | } |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 202 | |
| 203 | msg->status = ret; |
Phil Edworthy | ce32930 | 2012-11-22 14:37:27 +0000 | [diff] [blame] | 204 | if (!cs_change) { |
| 205 | ndelay(nsecs); |
| 206 | hspi_hw_cs_disable(hspi); |
| 207 | } |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 208 | spi_finalize_current_message(ctlr); |
Kuninori Morimoto | ec139b6 | 2012-03-14 02:47:40 -0700 | [diff] [blame] | 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 213 | static int hspi_probe(struct platform_device *pdev) |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 214 | { |
| 215 | struct resource *res; |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 216 | struct spi_controller *ctlr; |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 217 | struct hspi_priv *hspi; |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 218 | struct clk *clk; |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 219 | int ret; |
| 220 | |
| 221 | /* get base addr */ |
| 222 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 223 | if (!res) { |
| 224 | dev_err(&pdev->dev, "invalid resource\n"); |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 228 | ctlr = spi_alloc_master(&pdev->dev, sizeof(*hspi)); |
| 229 | if (!ctlr) |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 230 | return -ENOMEM; |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 231 | |
Simon Horman | 4a4dd7d | 2014-04-14 10:41:38 +0900 | [diff] [blame] | 232 | clk = clk_get(&pdev->dev, NULL); |
Cyril Roelandt | d3601e5 | 2012-12-12 01:24:54 +0100 | [diff] [blame] | 233 | if (IS_ERR(clk)) { |
Simon Horman | 4a4dd7d | 2014-04-14 10:41:38 +0900 | [diff] [blame] | 234 | dev_err(&pdev->dev, "couldn't get clock\n"); |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 235 | ret = -EINVAL; |
| 236 | goto error0; |
| 237 | } |
| 238 | |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 239 | hspi = spi_controller_get_devdata(ctlr); |
Jingoo Han | 24b5a82 | 2013-05-23 19:20:40 +0900 | [diff] [blame] | 240 | platform_set_drvdata(pdev, hspi); |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 241 | |
| 242 | /* init hspi */ |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 243 | hspi->ctlr = ctlr; |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 244 | hspi->dev = &pdev->dev; |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 245 | hspi->clk = clk; |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 246 | hspi->addr = devm_ioremap(hspi->dev, |
| 247 | res->start, resource_size(res)); |
| 248 | if (!hspi->addr) { |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 249 | ret = -ENOMEM; |
| 250 | goto error1; |
| 251 | } |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 252 | |
Kuninori Morimoto | 268d764 | 2013-10-03 02:15:50 -0700 | [diff] [blame] | 253 | pm_runtime_enable(&pdev->dev); |
| 254 | |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 255 | ctlr->bus_num = pdev->id; |
| 256 | ctlr->mode_bits = SPI_CPOL | SPI_CPHA; |
| 257 | ctlr->dev.of_node = pdev->dev.of_node; |
| 258 | ctlr->auto_runtime_pm = true; |
| 259 | ctlr->transfer_one_message = hspi_transfer_one_message; |
| 260 | ctlr->bits_per_word_mask = SPI_BPW_MASK(8); |
Axel Lin | 4522193 | 2014-02-12 22:09:52 +0800 | [diff] [blame] | 261 | |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 262 | ret = devm_spi_register_controller(&pdev->dev, ctlr); |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 263 | if (ret < 0) { |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 264 | dev_err(&pdev->dev, "devm_spi_register_controller error.\n"); |
Geert Uytterhoeven | 3abf0ed | 2014-03-11 10:59:10 +0100 | [diff] [blame] | 265 | goto error2; |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 268 | return 0; |
| 269 | |
Geert Uytterhoeven | 3abf0ed | 2014-03-11 10:59:10 +0100 | [diff] [blame] | 270 | error2: |
| 271 | pm_runtime_disable(&pdev->dev); |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 272 | error1: |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 273 | clk_put(clk); |
| 274 | error0: |
Geert Uytterhoeven | 5a0e577 | 2019-02-08 10:09:08 +0100 | [diff] [blame] | 275 | spi_controller_put(ctlr); |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 276 | |
| 277 | return ret; |
| 278 | } |
| 279 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 280 | static int hspi_remove(struct platform_device *pdev) |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 281 | { |
Jingoo Han | 24b5a82 | 2013-05-23 19:20:40 +0900 | [diff] [blame] | 282 | struct hspi_priv *hspi = platform_get_drvdata(pdev); |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 283 | |
| 284 | pm_runtime_disable(&pdev->dev); |
| 285 | |
Kuninori Morimoto | 49e599b8 | 2012-03-14 02:48:05 -0700 | [diff] [blame] | 286 | clk_put(hspi->clk); |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
Jingoo Han | 8e3489f | 2014-06-03 21:04:59 +0900 | [diff] [blame] | 291 | static const struct of_device_id hspi_of_match[] = { |
Kuninori Morimoto | e5f7825 | 2013-10-24 21:53:29 -0700 | [diff] [blame] | 292 | { .compatible = "renesas,hspi", }, |
| 293 | { /* sentinel */ } |
| 294 | }; |
| 295 | MODULE_DEVICE_TABLE(of, hspi_of_match); |
| 296 | |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 297 | static struct platform_driver hspi_driver = { |
| 298 | .probe = hspi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 299 | .remove = hspi_remove, |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 300 | .driver = { |
| 301 | .name = "sh-hspi", |
Kuninori Morimoto | e5f7825 | 2013-10-24 21:53:29 -0700 | [diff] [blame] | 302 | .of_match_table = hspi_of_match, |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 303 | }, |
| 304 | }; |
| 305 | module_platform_driver(hspi_driver); |
| 306 | |
| 307 | MODULE_DESCRIPTION("SuperH HSPI bus driver"); |
Wolfram Sang | 9135bac | 2018-08-22 00:02:23 +0200 | [diff] [blame] | 308 | MODULE_LICENSE("GPL v2"); |
Kuninori Morimoto | d1c8bbd | 2012-03-01 17:10:17 -0800 | [diff] [blame] | 309 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |
Axel Lin | caedb99 | 2014-01-08 18:52:40 +0800 | [diff] [blame] | 310 | MODULE_ALIAS("platform:sh-hspi"); |