spi-sh-msiof.c 18 KB

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