spi-sh-msiof.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * SuperH MSIOF SPI Master Interface
  3. *
  4. * Copyright (c) 2009 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/bitmap.h>
  12. #include <linux/clk.h>
  13. #include <linux/completion.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/gpio.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/spi/sh_msiof.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/spi/spi_bitbang.h>
  27. #include <asm/unaligned.h>
  28. struct sh_msiof_spi_priv {
  29. struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */
  30. void __iomem *mapbase;
  31. struct clk *clk;
  32. struct platform_device *pdev;
  33. struct sh_msiof_spi_info *info;
  34. struct completion done;
  35. unsigned long flags;
  36. int tx_fifo_size;
  37. int rx_fifo_size;
  38. };
  39. #define TMDR1 0x00
  40. #define TMDR2 0x04
  41. #define TMDR3 0x08
  42. #define RMDR1 0x10
  43. #define RMDR2 0x14
  44. #define RMDR3 0x18
  45. #define TSCR 0x20
  46. #define RSCR 0x22
  47. #define CTR 0x28
  48. #define FCTR 0x30
  49. #define STR 0x40
  50. #define IER 0x44
  51. #define TDR1 0x48
  52. #define TDR2 0x4c
  53. #define TFDR 0x50
  54. #define RDR1 0x58
  55. #define RDR2 0x5c
  56. #define RFDR 0x60
  57. #define CTR_TSCKE (1 << 15)
  58. #define CTR_TFSE (1 << 14)
  59. #define CTR_TXE (1 << 9)
  60. #define CTR_RXE (1 << 8)
  61. #define STR_TEOF (1 << 23)
  62. #define STR_REOF (1 << 7)
  63. static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
  64. {
  65. switch (reg_offs) {
  66. case TSCR:
  67. case RSCR:
  68. return ioread16(p->mapbase + reg_offs);
  69. default:
  70. return ioread32(p->mapbase + reg_offs);
  71. }
  72. }
  73. static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
  74. u32 value)
  75. {
  76. switch (reg_offs) {
  77. case TSCR:
  78. case RSCR:
  79. iowrite16(value, p->mapbase + reg_offs);
  80. break;
  81. default:
  82. iowrite32(value, p->mapbase + reg_offs);
  83. break;
  84. }
  85. }
  86. static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
  87. u32 clr, u32 set)
  88. {
  89. u32 mask = clr | set;
  90. u32 data;
  91. int k;
  92. data = sh_msiof_read(p, CTR);
  93. data &= ~clr;
  94. data |= set;
  95. sh_msiof_write(p, CTR, data);
  96. for (k = 100; k > 0; k--) {
  97. if ((sh_msiof_read(p, CTR) & mask) == set)
  98. break;
  99. udelay(10);
  100. }
  101. return k > 0 ? 0 : -ETIMEDOUT;
  102. }
  103. static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
  104. {
  105. struct sh_msiof_spi_priv *p = data;
  106. /* just disable the interrupt and wake up */
  107. sh_msiof_write(p, IER, 0);
  108. complete(&p->done);
  109. return IRQ_HANDLED;
  110. }
  111. static struct {
  112. unsigned short div;
  113. unsigned short scr;
  114. } const sh_msiof_spi_clk_table[] = {
  115. { 1, 0x0007 },
  116. { 2, 0x0000 },
  117. { 4, 0x0001 },
  118. { 8, 0x0002 },
  119. { 16, 0x0003 },
  120. { 32, 0x0004 },
  121. { 64, 0x1f00 },
  122. { 128, 0x1f01 },
  123. { 256, 0x1f02 },
  124. { 512, 0x1f03 },
  125. { 1024, 0x1f04 },
  126. };
  127. static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
  128. unsigned long parent_rate,
  129. unsigned long spi_hz)
  130. {
  131. unsigned long div = 1024;
  132. size_t k;
  133. if (!WARN_ON(!spi_hz || !parent_rate))
  134. div = DIV_ROUND_UP(parent_rate, spi_hz);
  135. /* TODO: make more fine grained */
  136. for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
  137. if (sh_msiof_spi_clk_table[k].div >= div)
  138. break;
  139. }
  140. k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);
  141. sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
  142. sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
  143. }
  144. static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
  145. u32 cpol, u32 cpha,
  146. u32 tx_hi_z, u32 lsb_first, u32 cs_high)
  147. {
  148. u32 tmp;
  149. int edge;
  150. /*
  151. * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG
  152. * 0 0 10 10 1 1
  153. * 0 1 10 10 0 0
  154. * 1 0 11 11 0 0
  155. * 1 1 11 11 1 1
  156. */
  157. sh_msiof_write(p, FCTR, 0);
  158. tmp = 0;
  159. tmp |= !cs_high << 25;
  160. tmp |= lsb_first << 24;
  161. sh_msiof_write(p, TMDR1, 0xe0000005 | tmp);
  162. sh_msiof_write(p, RMDR1, 0x20000005 | tmp);
  163. tmp = 0xa0000000;
  164. tmp |= cpol << 30; /* TSCKIZ */
  165. tmp |= cpol << 28; /* RSCKIZ */
  166. edge = cpol ^ !cpha;
  167. tmp |= edge << 27; /* TEDG */
  168. tmp |= edge << 26; /* REDG */
  169. tmp |= (tx_hi_z ? 2 : 0) << 22; /* TXDIZ */
  170. sh_msiof_write(p, CTR, tmp);
  171. }
  172. static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
  173. const void *tx_buf, void *rx_buf,
  174. u32 bits, u32 words)
  175. {
  176. u32 dr2 = ((bits - 1) << 24) | ((words - 1) << 16);
  177. if (tx_buf)
  178. sh_msiof_write(p, TMDR2, dr2);
  179. else
  180. sh_msiof_write(p, TMDR2, dr2 | 1);
  181. if (rx_buf)
  182. sh_msiof_write(p, RMDR2, dr2);
  183. sh_msiof_write(p, IER, STR_TEOF | STR_REOF);
  184. }
  185. static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
  186. {
  187. sh_msiof_write(p, STR, sh_msiof_read(p, STR));
  188. }
  189. static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
  190. const void *tx_buf, int words, int fs)
  191. {
  192. const u8 *buf_8 = tx_buf;
  193. int k;
  194. for (k = 0; k < words; k++)
  195. sh_msiof_write(p, TFDR, buf_8[k] << fs);
  196. }
  197. static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
  198. const void *tx_buf, int words, int fs)
  199. {
  200. const u16 *buf_16 = tx_buf;
  201. int k;
  202. for (k = 0; k < words; k++)
  203. sh_msiof_write(p, TFDR, buf_16[k] << fs);
  204. }
  205. static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
  206. const void *tx_buf, int words, int fs)
  207. {
  208. const u16 *buf_16 = tx_buf;
  209. int k;
  210. for (k = 0; k < words; k++)
  211. sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
  212. }
  213. static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
  214. const void *tx_buf, int words, int fs)
  215. {
  216. const u32 *buf_32 = tx_buf;
  217. int k;
  218. for (k = 0; k < words; k++)
  219. sh_msiof_write(p, TFDR, buf_32[k] << fs);
  220. }
  221. static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
  222. const void *tx_buf, int words, int fs)
  223. {
  224. const u32 *buf_32 = tx_buf;
  225. int k;
  226. for (k = 0; k < words; k++)
  227. sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
  228. }
  229. static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
  230. const void *tx_buf, int words, int fs)
  231. {
  232. const u32 *buf_32 = tx_buf;
  233. int k;
  234. for (k = 0; k < words; k++)
  235. sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs));
  236. }
  237. static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
  238. const void *tx_buf, int words, int fs)
  239. {
  240. const u32 *buf_32 = tx_buf;
  241. int k;
  242. for (k = 0; k < words; k++)
  243. sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs));
  244. }
  245. static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
  246. void *rx_buf, int words, int fs)
  247. {
  248. u8 *buf_8 = rx_buf;
  249. int k;
  250. for (k = 0; k < words; k++)
  251. buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
  252. }
  253. static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
  254. void *rx_buf, int words, int fs)
  255. {
  256. u16 *buf_16 = rx_buf;
  257. int k;
  258. for (k = 0; k < words; k++)
  259. buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
  260. }
  261. static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
  262. void *rx_buf, int words, int fs)
  263. {
  264. u16 *buf_16 = rx_buf;
  265. int k;
  266. for (k = 0; k < words; k++)
  267. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
  268. }
  269. static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
  270. void *rx_buf, int words, int fs)
  271. {
  272. u32 *buf_32 = rx_buf;
  273. int k;
  274. for (k = 0; k < words; k++)
  275. buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
  276. }
  277. static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
  278. void *rx_buf, int words, int fs)
  279. {
  280. u32 *buf_32 = rx_buf;
  281. int k;
  282. for (k = 0; k < words; k++)
  283. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
  284. }
  285. static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
  286. void *rx_buf, int words, int fs)
  287. {
  288. u32 *buf_32 = rx_buf;
  289. int k;
  290. for (k = 0; k < words; k++)
  291. buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs);
  292. }
  293. static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
  294. void *rx_buf, int words, int fs)
  295. {
  296. u32 *buf_32 = rx_buf;
  297. int k;
  298. for (k = 0; k < words; k++)
  299. put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]);
  300. }
  301. static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t)
  302. {
  303. int bits;
  304. bits = t ? t->bits_per_word : 0;
  305. if (!bits)
  306. bits = spi->bits_per_word;
  307. return bits;
  308. }
  309. static unsigned long sh_msiof_spi_hz(struct spi_device *spi,
  310. struct spi_transfer *t)
  311. {
  312. unsigned long hz;
  313. hz = t ? t->speed_hz : 0;
  314. if (!hz)
  315. hz = spi->max_speed_hz;
  316. return hz;
  317. }
  318. static int sh_msiof_spi_setup_transfer(struct spi_device *spi,
  319. struct spi_transfer *t)
  320. {
  321. int bits;
  322. /* noting to check hz values against since parent clock is disabled */
  323. bits = sh_msiof_spi_bits(spi, t);
  324. if (bits < 8)
  325. return -EINVAL;
  326. if (bits > 32)
  327. return -EINVAL;
  328. return spi_bitbang_setup_transfer(spi, t);
  329. }
  330. static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
  331. {
  332. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  333. int value;
  334. /* chip select is active low unless SPI_CS_HIGH is set */
  335. if (spi->mode & SPI_CS_HIGH)
  336. value = (is_on == BITBANG_CS_ACTIVE) ? 1 : 0;
  337. else
  338. value = (is_on == BITBANG_CS_ACTIVE) ? 0 : 1;
  339. if (is_on == BITBANG_CS_ACTIVE) {
  340. if (!test_and_set_bit(0, &p->flags)) {
  341. pm_runtime_get_sync(&p->pdev->dev);
  342. clk_enable(p->clk);
  343. }
  344. /* Configure pins before asserting CS */
  345. sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
  346. !!(spi->mode & SPI_CPHA),
  347. !!(spi->mode & SPI_3WIRE),
  348. !!(spi->mode & SPI_LSB_FIRST),
  349. !!(spi->mode & SPI_CS_HIGH));
  350. }
  351. /* use spi->controller data for CS (same strategy as spi_gpio) */
  352. gpio_set_value((uintptr_t)spi->controller_data, value);
  353. if (is_on == BITBANG_CS_INACTIVE) {
  354. if (test_and_clear_bit(0, &p->flags)) {
  355. clk_disable(p->clk);
  356. pm_runtime_put(&p->pdev->dev);
  357. }
  358. }
  359. }
  360. static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
  361. void (*tx_fifo)(struct sh_msiof_spi_priv *,
  362. const void *, int, int),
  363. void (*rx_fifo)(struct sh_msiof_spi_priv *,
  364. void *, int, int),
  365. const void *tx_buf, void *rx_buf,
  366. int words, int bits)
  367. {
  368. int fifo_shift;
  369. int ret;
  370. /* limit maximum word transfer to rx/tx fifo size */
  371. if (tx_buf)
  372. words = min_t(int, words, p->tx_fifo_size);
  373. if (rx_buf)
  374. words = min_t(int, words, p->rx_fifo_size);
  375. /* the fifo contents need shifting */
  376. fifo_shift = 32 - bits;
  377. /* setup msiof transfer mode registers */
  378. sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
  379. /* write tx fifo */
  380. if (tx_buf)
  381. tx_fifo(p, tx_buf, words, fifo_shift);
  382. /* setup clock and rx/tx signals */
  383. ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
  384. if (rx_buf)
  385. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
  386. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
  387. /* start by setting frame bit */
  388. reinit_completion(&p->done);
  389. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
  390. if (ret) {
  391. dev_err(&p->pdev->dev, "failed to start hardware\n");
  392. goto err;
  393. }
  394. /* wait for tx fifo to be emptied / rx fifo to be filled */
  395. wait_for_completion(&p->done);
  396. /* read rx fifo */
  397. if (rx_buf)
  398. rx_fifo(p, rx_buf, words, fifo_shift);
  399. /* clear status bits */
  400. sh_msiof_reset_str(p);
  401. /* shut down frame, tx/tx and clock signals */
  402. ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
  403. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
  404. if (rx_buf)
  405. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
  406. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
  407. if (ret) {
  408. dev_err(&p->pdev->dev, "failed to shut down hardware\n");
  409. goto err;
  410. }
  411. return words;
  412. err:
  413. sh_msiof_write(p, IER, 0);
  414. return ret;
  415. }
  416. static int sh_msiof_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  417. {
  418. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  419. void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
  420. void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
  421. int bits;
  422. int bytes_per_word;
  423. int bytes_done;
  424. int words;
  425. int n;
  426. bool swab;
  427. bits = sh_msiof_spi_bits(spi, t);
  428. if (bits <= 8 && t->len > 15 && !(t->len & 3)) {
  429. bits = 32;
  430. swab = true;
  431. } else {
  432. swab = false;
  433. }
  434. /* setup bytes per word and fifo read/write functions */
  435. if (bits <= 8) {
  436. bytes_per_word = 1;
  437. tx_fifo = sh_msiof_spi_write_fifo_8;
  438. rx_fifo = sh_msiof_spi_read_fifo_8;
  439. } else if (bits <= 16) {
  440. bytes_per_word = 2;
  441. if ((unsigned long)t->tx_buf & 0x01)
  442. tx_fifo = sh_msiof_spi_write_fifo_16u;
  443. else
  444. tx_fifo = sh_msiof_spi_write_fifo_16;
  445. if ((unsigned long)t->rx_buf & 0x01)
  446. rx_fifo = sh_msiof_spi_read_fifo_16u;
  447. else
  448. rx_fifo = sh_msiof_spi_read_fifo_16;
  449. } else if (swab) {
  450. bytes_per_word = 4;
  451. if ((unsigned long)t->tx_buf & 0x03)
  452. tx_fifo = sh_msiof_spi_write_fifo_s32u;
  453. else
  454. tx_fifo = sh_msiof_spi_write_fifo_s32;
  455. if ((unsigned long)t->rx_buf & 0x03)
  456. rx_fifo = sh_msiof_spi_read_fifo_s32u;
  457. else
  458. rx_fifo = sh_msiof_spi_read_fifo_s32;
  459. } else {
  460. bytes_per_word = 4;
  461. if ((unsigned long)t->tx_buf & 0x03)
  462. tx_fifo = sh_msiof_spi_write_fifo_32u;
  463. else
  464. tx_fifo = sh_msiof_spi_write_fifo_32;
  465. if ((unsigned long)t->rx_buf & 0x03)
  466. rx_fifo = sh_msiof_spi_read_fifo_32u;
  467. else
  468. rx_fifo = sh_msiof_spi_read_fifo_32;
  469. }
  470. /* setup clocks (clock already enabled in chipselect()) */
  471. sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk),
  472. sh_msiof_spi_hz(spi, t));
  473. /* transfer in fifo sized chunks */
  474. words = t->len / bytes_per_word;
  475. bytes_done = 0;
  476. while (bytes_done < t->len) {
  477. void *rx_buf = t->rx_buf ? t->rx_buf + bytes_done : NULL;
  478. const void *tx_buf = t->tx_buf ? t->tx_buf + bytes_done : NULL;
  479. n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo,
  480. tx_buf,
  481. rx_buf,
  482. words, bits);
  483. if (n < 0)
  484. break;
  485. bytes_done += n * bytes_per_word;
  486. words -= n;
  487. }
  488. return bytes_done;
  489. }
  490. static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
  491. u32 word, u8 bits)
  492. {
  493. BUG(); /* unused but needed by bitbang code */
  494. return 0;
  495. }
  496. #ifdef CONFIG_OF
  497. static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
  498. {
  499. struct sh_msiof_spi_info *info;
  500. struct device_node *np = dev->of_node;
  501. u32 num_cs = 0;
  502. info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL);
  503. if (!info) {
  504. dev_err(dev, "failed to allocate setup data\n");
  505. return NULL;
  506. }
  507. /* Parse the MSIOF properties */
  508. of_property_read_u32(np, "num-cs", &num_cs);
  509. of_property_read_u32(np, "renesas,tx-fifo-size",
  510. &info->tx_fifo_override);
  511. of_property_read_u32(np, "renesas,rx-fifo-size",
  512. &info->rx_fifo_override);
  513. info->num_chipselect = num_cs;
  514. return info;
  515. }
  516. #else
  517. static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
  518. {
  519. return NULL;
  520. }
  521. #endif
  522. static int sh_msiof_spi_probe(struct platform_device *pdev)
  523. {
  524. struct resource *r;
  525. struct spi_master *master;
  526. struct sh_msiof_spi_priv *p;
  527. int i;
  528. int ret;
  529. master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
  530. if (master == NULL) {
  531. dev_err(&pdev->dev, "failed to allocate spi master\n");
  532. return -ENOMEM;
  533. }
  534. p = spi_master_get_devdata(master);
  535. platform_set_drvdata(pdev, p);
  536. if (pdev->dev.of_node)
  537. p->info = sh_msiof_spi_parse_dt(&pdev->dev);
  538. else
  539. p->info = dev_get_platdata(&pdev->dev);
  540. if (!p->info) {
  541. dev_err(&pdev->dev, "failed to obtain device info\n");
  542. ret = -ENXIO;
  543. goto err1;
  544. }
  545. init_completion(&p->done);
  546. p->clk = devm_clk_get(&pdev->dev, NULL);
  547. if (IS_ERR(p->clk)) {
  548. dev_err(&pdev->dev, "cannot get clock\n");
  549. ret = PTR_ERR(p->clk);
  550. goto err1;
  551. }
  552. i = platform_get_irq(pdev, 0);
  553. if (i < 0) {
  554. dev_err(&pdev->dev, "cannot get platform IRQ\n");
  555. ret = -ENOENT;
  556. goto err1;
  557. }
  558. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  559. p->mapbase = devm_ioremap_resource(&pdev->dev, r);
  560. if (IS_ERR(p->mapbase)) {
  561. ret = PTR_ERR(p->mapbase);
  562. goto err1;
  563. }
  564. ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0,
  565. dev_name(&pdev->dev), p);
  566. if (ret) {
  567. dev_err(&pdev->dev, "unable to request irq\n");
  568. goto err1;
  569. }
  570. ret = clk_prepare(p->clk);
  571. if (ret < 0) {
  572. dev_err(&pdev->dev, "unable to prepare clock\n");
  573. goto err1;
  574. }
  575. p->pdev = pdev;
  576. pm_runtime_enable(&pdev->dev);
  577. /* The standard version of MSIOF use 64 word FIFOs */
  578. p->tx_fifo_size = 64;
  579. p->rx_fifo_size = 64;
  580. /* Platform data may override FIFO sizes */
  581. if (p->info->tx_fifo_override)
  582. p->tx_fifo_size = p->info->tx_fifo_override;
  583. if (p->info->rx_fifo_override)
  584. p->rx_fifo_size = p->info->rx_fifo_override;
  585. /* init master and bitbang code */
  586. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  587. master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
  588. master->flags = 0;
  589. master->bus_num = pdev->id;
  590. master->num_chipselect = p->info->num_chipselect;
  591. master->setup = spi_bitbang_setup;
  592. master->cleanup = spi_bitbang_cleanup;
  593. p->bitbang.master = master;
  594. p->bitbang.chipselect = sh_msiof_spi_chipselect;
  595. p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
  596. p->bitbang.txrx_bufs = sh_msiof_spi_txrx;
  597. p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word;
  598. p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word;
  599. p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word;
  600. p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word;
  601. ret = spi_bitbang_start(&p->bitbang);
  602. if (ret == 0)
  603. return 0;
  604. pm_runtime_disable(&pdev->dev);
  605. clk_unprepare(p->clk);
  606. err1:
  607. spi_master_put(master);
  608. return ret;
  609. }
  610. static int sh_msiof_spi_remove(struct platform_device *pdev)
  611. {
  612. struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
  613. int ret;
  614. ret = spi_bitbang_stop(&p->bitbang);
  615. if (!ret) {
  616. pm_runtime_disable(&pdev->dev);
  617. clk_unprepare(p->clk);
  618. spi_master_put(p->bitbang.master);
  619. }
  620. return ret;
  621. }
  622. #ifdef CONFIG_OF
  623. static const struct of_device_id sh_msiof_match[] = {
  624. { .compatible = "renesas,sh-msiof", },
  625. { .compatible = "renesas,sh-mobile-msiof", },
  626. {},
  627. };
  628. MODULE_DEVICE_TABLE(of, sh_msiof_match);
  629. #endif
  630. static struct platform_driver sh_msiof_spi_drv = {
  631. .probe = sh_msiof_spi_probe,
  632. .remove = sh_msiof_spi_remove,
  633. .driver = {
  634. .name = "spi_sh_msiof",
  635. .owner = THIS_MODULE,
  636. .of_match_table = of_match_ptr(sh_msiof_match),
  637. },
  638. };
  639. module_platform_driver(sh_msiof_spi_drv);
  640. MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
  641. MODULE_AUTHOR("Magnus Damm");
  642. MODULE_LICENSE("GPL v2");
  643. MODULE_ALIAS("platform:spi_sh_msiof");