spi-sh.c 12 KB

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