spi-sh.c 12 KB

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