spi-sh.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SH SPI bus driver
  4. *
  5. * Copyright (C) 2011 Renesas Solutions Corp.
  6. *
  7. * Based on pxa2xx_spi.c:
  8. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/errno.h>
  14. #include <linux/timer.h>
  15. #include <linux/delay.h>
  16. #include <linux/list.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/io.h>
  21. #include <linux/spi/spi.h>
  22. #define SPI_SH_TBR 0x00
  23. #define SPI_SH_RBR 0x00
  24. #define SPI_SH_CR1 0x08
  25. #define SPI_SH_CR2 0x10
  26. #define SPI_SH_CR3 0x18
  27. #define SPI_SH_CR4 0x20
  28. #define SPI_SH_CR5 0x28
  29. /* CR1 */
  30. #define SPI_SH_TBE 0x80
  31. #define SPI_SH_TBF 0x40
  32. #define SPI_SH_RBE 0x20
  33. #define SPI_SH_RBF 0x10
  34. #define SPI_SH_PFONRD 0x08
  35. #define SPI_SH_SSDB 0x04
  36. #define SPI_SH_SSD 0x02
  37. #define SPI_SH_SSA 0x01
  38. /* CR2 */
  39. #define SPI_SH_RSTF 0x80
  40. #define SPI_SH_LOOPBK 0x40
  41. #define SPI_SH_CPOL 0x20
  42. #define SPI_SH_CPHA 0x10
  43. #define SPI_SH_L1M0 0x08
  44. /* CR3 */
  45. #define SPI_SH_MAX_BYTE 0xFF
  46. /* CR4 */
  47. #define SPI_SH_TBEI 0x80
  48. #define SPI_SH_TBFI 0x40
  49. #define SPI_SH_RBEI 0x20
  50. #define SPI_SH_RBFI 0x10
  51. #define SPI_SH_WPABRT 0x04
  52. #define SPI_SH_SSS 0x01
  53. /* CR8 */
  54. #define SPI_SH_P1L0 0x80
  55. #define SPI_SH_PP1L0 0x40
  56. #define SPI_SH_MUXI 0x20
  57. #define SPI_SH_MUXIRQ 0x10
  58. #define SPI_SH_FIFO_SIZE 32
  59. #define SPI_SH_SEND_TIMEOUT (3 * HZ)
  60. #define SPI_SH_RECEIVE_TIMEOUT (HZ >> 3)
  61. #undef DEBUG
  62. struct spi_sh_data {
  63. void __iomem *addr;
  64. int irq;
  65. struct spi_master *master;
  66. unsigned long cr1;
  67. wait_queue_head_t wait;
  68. int width;
  69. };
  70. static void spi_sh_write(struct spi_sh_data *ss, unsigned long data,
  71. unsigned long offset)
  72. {
  73. if (ss->width == 8)
  74. iowrite8(data, ss->addr + (offset >> 2));
  75. else if (ss->width == 32)
  76. iowrite32(data, ss->addr + offset);
  77. }
  78. static unsigned long spi_sh_read(struct spi_sh_data *ss, unsigned long offset)
  79. {
  80. if (ss->width == 8)
  81. return ioread8(ss->addr + (offset >> 2));
  82. else if (ss->width == 32)
  83. return ioread32(ss->addr + offset);
  84. else
  85. return 0;
  86. }
  87. static void spi_sh_set_bit(struct spi_sh_data *ss, unsigned long val,
  88. unsigned long offset)
  89. {
  90. unsigned long tmp;
  91. tmp = spi_sh_read(ss, offset);
  92. tmp |= val;
  93. spi_sh_write(ss, tmp, offset);
  94. }
  95. static void spi_sh_clear_bit(struct spi_sh_data *ss, unsigned long val,
  96. unsigned long offset)
  97. {
  98. unsigned long tmp;
  99. tmp = spi_sh_read(ss, offset);
  100. tmp &= ~val;
  101. spi_sh_write(ss, tmp, offset);
  102. }
  103. static void clear_fifo(struct spi_sh_data *ss)
  104. {
  105. spi_sh_set_bit(ss, SPI_SH_RSTF, SPI_SH_CR2);
  106. spi_sh_clear_bit(ss, SPI_SH_RSTF, SPI_SH_CR2);
  107. }
  108. static int spi_sh_wait_receive_buffer(struct spi_sh_data *ss)
  109. {
  110. int timeout = 100000;
  111. while (spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_RBE) {
  112. udelay(10);
  113. if (timeout-- < 0)
  114. return -ETIMEDOUT;
  115. }
  116. return 0;
  117. }
  118. static int spi_sh_wait_write_buffer_empty(struct spi_sh_data *ss)
  119. {
  120. int timeout = 100000;
  121. while (!(spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_TBE)) {
  122. udelay(10);
  123. if (timeout-- < 0)
  124. return -ETIMEDOUT;
  125. }
  126. return 0;
  127. }
  128. static int spi_sh_send(struct spi_sh_data *ss, struct spi_message *mesg,
  129. struct spi_transfer *t)
  130. {
  131. int i, retval = 0;
  132. int remain = t->len;
  133. int cur_len;
  134. unsigned char *data;
  135. long ret;
  136. if (t->len)
  137. spi_sh_set_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
  138. data = (unsigned char *)t->tx_buf;
  139. while (remain > 0) {
  140. cur_len = min(SPI_SH_FIFO_SIZE, remain);
  141. for (i = 0; i < cur_len &&
  142. !(spi_sh_read(ss, SPI_SH_CR4) &
  143. SPI_SH_WPABRT) &&
  144. !(spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_TBF);
  145. i++)
  146. spi_sh_write(ss, (unsigned long)data[i], SPI_SH_TBR);
  147. if (spi_sh_read(ss, SPI_SH_CR4) & SPI_SH_WPABRT) {
  148. /* Abort SPI operation */
  149. spi_sh_set_bit(ss, SPI_SH_WPABRT, SPI_SH_CR4);
  150. retval = -EIO;
  151. break;
  152. }
  153. cur_len = i;
  154. remain -= cur_len;
  155. data += cur_len;
  156. if (remain > 0) {
  157. ss->cr1 &= ~SPI_SH_TBE;
  158. spi_sh_set_bit(ss, SPI_SH_TBE, SPI_SH_CR4);
  159. ret = wait_event_interruptible_timeout(ss->wait,
  160. ss->cr1 & SPI_SH_TBE,
  161. SPI_SH_SEND_TIMEOUT);
  162. if (ret == 0 && !(ss->cr1 & SPI_SH_TBE)) {
  163. printk(KERN_ERR "%s: timeout\n", __func__);
  164. return -ETIMEDOUT;
  165. }
  166. }
  167. }
  168. if (list_is_last(&t->transfer_list, &mesg->transfers)) {
  169. spi_sh_clear_bit(ss, SPI_SH_SSD | SPI_SH_SSDB, SPI_SH_CR1);
  170. spi_sh_set_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
  171. ss->cr1 &= ~SPI_SH_TBE;
  172. spi_sh_set_bit(ss, SPI_SH_TBE, SPI_SH_CR4);
  173. ret = wait_event_interruptible_timeout(ss->wait,
  174. ss->cr1 & SPI_SH_TBE,
  175. SPI_SH_SEND_TIMEOUT);
  176. if (ret == 0 && (ss->cr1 & SPI_SH_TBE)) {
  177. printk(KERN_ERR "%s: timeout\n", __func__);
  178. return -ETIMEDOUT;
  179. }
  180. }
  181. return retval;
  182. }
  183. static int spi_sh_receive(struct spi_sh_data *ss, struct spi_message *mesg,
  184. struct spi_transfer *t)
  185. {
  186. int i;
  187. int remain = t->len;
  188. int cur_len;
  189. unsigned char *data;
  190. long ret;
  191. if (t->len > SPI_SH_MAX_BYTE)
  192. spi_sh_write(ss, SPI_SH_MAX_BYTE, SPI_SH_CR3);
  193. else
  194. spi_sh_write(ss, t->len, SPI_SH_CR3);
  195. spi_sh_clear_bit(ss, SPI_SH_SSD | SPI_SH_SSDB, SPI_SH_CR1);
  196. spi_sh_set_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
  197. spi_sh_wait_write_buffer_empty(ss);
  198. data = (unsigned char *)t->rx_buf;
  199. while (remain > 0) {
  200. if (remain >= SPI_SH_FIFO_SIZE) {
  201. ss->cr1 &= ~SPI_SH_RBF;
  202. spi_sh_set_bit(ss, SPI_SH_RBF, SPI_SH_CR4);
  203. ret = wait_event_interruptible_timeout(ss->wait,
  204. ss->cr1 & SPI_SH_RBF,
  205. SPI_SH_RECEIVE_TIMEOUT);
  206. if (ret == 0 &&
  207. spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_RBE) {
  208. printk(KERN_ERR "%s: timeout\n", __func__);
  209. return -ETIMEDOUT;
  210. }
  211. }
  212. cur_len = min(SPI_SH_FIFO_SIZE, remain);
  213. for (i = 0; i < cur_len; i++) {
  214. if (spi_sh_wait_receive_buffer(ss))
  215. break;
  216. data[i] = (unsigned char)spi_sh_read(ss, SPI_SH_RBR);
  217. }
  218. remain -= cur_len;
  219. data += cur_len;
  220. }
  221. /* deassert CS when SPI is receiving. */
  222. if (t->len > SPI_SH_MAX_BYTE) {
  223. clear_fifo(ss);
  224. spi_sh_write(ss, 1, SPI_SH_CR3);
  225. } else {
  226. spi_sh_write(ss, 0, SPI_SH_CR3);
  227. }
  228. return 0;
  229. }
  230. static int spi_sh_transfer_one_message(struct spi_controller *ctlr,
  231. struct spi_message *mesg)
  232. {
  233. struct spi_sh_data *ss = spi_controller_get_devdata(ctlr);
  234. struct spi_transfer *t;
  235. int ret;
  236. pr_debug("%s: enter\n", __func__);
  237. spi_sh_clear_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
  238. list_for_each_entry(t, &mesg->transfers, transfer_list) {
  239. pr_debug("tx_buf = %p, rx_buf = %p\n",
  240. t->tx_buf, t->rx_buf);
  241. pr_debug("len = %d, delay.value = %d\n",
  242. t->len, t->delay.value);
  243. if (t->tx_buf) {
  244. ret = spi_sh_send(ss, mesg, t);
  245. if (ret < 0)
  246. goto error;
  247. }
  248. if (t->rx_buf) {
  249. ret = spi_sh_receive(ss, mesg, t);
  250. if (ret < 0)
  251. goto error;
  252. }
  253. mesg->actual_length += t->len;
  254. }
  255. mesg->status = 0;
  256. spi_finalize_current_message(ctlr);
  257. clear_fifo(ss);
  258. spi_sh_set_bit(ss, SPI_SH_SSD, SPI_SH_CR1);
  259. udelay(100);
  260. spi_sh_clear_bit(ss, SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD,
  261. SPI_SH_CR1);
  262. clear_fifo(ss);
  263. return 0;
  264. error:
  265. mesg->status = ret;
  266. spi_finalize_current_message(ctlr);
  267. if (mesg->complete)
  268. mesg->complete(mesg->context);
  269. spi_sh_clear_bit(ss, SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD,
  270. SPI_SH_CR1);
  271. clear_fifo(ss);
  272. return ret;
  273. }
  274. static int spi_sh_setup(struct spi_device *spi)
  275. {
  276. struct spi_sh_data *ss = spi_master_get_devdata(spi->master);
  277. pr_debug("%s: enter\n", __func__);
  278. spi_sh_write(ss, 0xfe, SPI_SH_CR1); /* SPI sycle stop */
  279. spi_sh_write(ss, 0x00, SPI_SH_CR1); /* CR1 init */
  280. spi_sh_write(ss, 0x00, SPI_SH_CR3); /* CR3 init */
  281. clear_fifo(ss);
  282. /* 1/8 clock */
  283. spi_sh_write(ss, spi_sh_read(ss, SPI_SH_CR2) | 0x07, SPI_SH_CR2);
  284. udelay(10);
  285. return 0;
  286. }
  287. static void spi_sh_cleanup(struct spi_device *spi)
  288. {
  289. struct spi_sh_data *ss = spi_master_get_devdata(spi->master);
  290. pr_debug("%s: enter\n", __func__);
  291. spi_sh_clear_bit(ss, SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD,
  292. SPI_SH_CR1);
  293. }
  294. static irqreturn_t spi_sh_irq(int irq, void *_ss)
  295. {
  296. struct spi_sh_data *ss = (struct spi_sh_data *)_ss;
  297. unsigned long cr1;
  298. cr1 = spi_sh_read(ss, SPI_SH_CR1);
  299. if (cr1 & SPI_SH_TBE)
  300. ss->cr1 |= SPI_SH_TBE;
  301. if (cr1 & SPI_SH_TBF)
  302. ss->cr1 |= SPI_SH_TBF;
  303. if (cr1 & SPI_SH_RBE)
  304. ss->cr1 |= SPI_SH_RBE;
  305. if (cr1 & SPI_SH_RBF)
  306. ss->cr1 |= SPI_SH_RBF;
  307. if (ss->cr1) {
  308. spi_sh_clear_bit(ss, ss->cr1, SPI_SH_CR4);
  309. wake_up(&ss->wait);
  310. }
  311. return IRQ_HANDLED;
  312. }
  313. static int spi_sh_remove(struct platform_device *pdev)
  314. {
  315. struct spi_sh_data *ss = platform_get_drvdata(pdev);
  316. spi_unregister_master(ss->master);
  317. free_irq(ss->irq, ss);
  318. return 0;
  319. }
  320. static int spi_sh_probe(struct platform_device *pdev)
  321. {
  322. struct resource *res;
  323. struct spi_master *master;
  324. struct spi_sh_data *ss;
  325. int ret, irq;
  326. /* get base addr */
  327. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  328. if (unlikely(res == NULL)) {
  329. dev_err(&pdev->dev, "invalid resource\n");
  330. return -EINVAL;
  331. }
  332. irq = platform_get_irq(pdev, 0);
  333. if (irq < 0)
  334. return irq;
  335. master = devm_spi_alloc_master(&pdev->dev, sizeof(struct spi_sh_data));
  336. if (master == NULL) {
  337. dev_err(&pdev->dev, "spi_alloc_master error.\n");
  338. return -ENOMEM;
  339. }
  340. ss = spi_master_get_devdata(master);
  341. platform_set_drvdata(pdev, ss);
  342. switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
  343. case IORESOURCE_MEM_8BIT:
  344. ss->width = 8;
  345. break;
  346. case IORESOURCE_MEM_32BIT:
  347. ss->width = 32;
  348. break;
  349. default:
  350. dev_err(&pdev->dev, "No support width\n");
  351. return -ENODEV;
  352. }
  353. ss->irq = irq;
  354. ss->master = master;
  355. ss->addr = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  356. if (ss->addr == NULL) {
  357. dev_err(&pdev->dev, "ioremap error.\n");
  358. return -ENOMEM;
  359. }
  360. init_waitqueue_head(&ss->wait);
  361. ret = request_irq(irq, spi_sh_irq, 0, "spi_sh", ss);
  362. if (ret < 0) {
  363. dev_err(&pdev->dev, "request_irq error\n");
  364. return ret;
  365. }
  366. master->num_chipselect = 2;
  367. master->bus_num = pdev->id;
  368. master->setup = spi_sh_setup;
  369. master->transfer_one_message = spi_sh_transfer_one_message;
  370. master->cleanup = spi_sh_cleanup;
  371. ret = spi_register_master(master);
  372. if (ret < 0) {
  373. printk(KERN_ERR "spi_register_master error.\n");
  374. goto error3;
  375. }
  376. return 0;
  377. error3:
  378. free_irq(irq, ss);
  379. return ret;
  380. }
  381. static struct platform_driver spi_sh_driver = {
  382. .probe = spi_sh_probe,
  383. .remove = spi_sh_remove,
  384. .driver = {
  385. .name = "sh_spi",
  386. },
  387. };
  388. module_platform_driver(spi_sh_driver);
  389. MODULE_DESCRIPTION("SH SPI bus driver");
  390. MODULE_LICENSE("GPL v2");
  391. MODULE_AUTHOR("Yoshihiro Shimoda");
  392. MODULE_ALIAS("platform:sh_spi");