spi-sh-msiof.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 = DIV_ROUND_UP(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, u32 cs_high)
  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. tmp = 0;
  160. tmp |= !cs_high << 25;
  161. tmp |= lsb_first << 24;
  162. sh_msiof_write(p, TMDR1, 0xe0000005 | tmp);
  163. sh_msiof_write(p, RMDR1, 0x20000005 | tmp);
  164. tmp = 0xa0000000;
  165. tmp |= cpol << 30; /* TSCKIZ */
  166. tmp |= cpol << 28; /* RSCKIZ */
  167. edge = cpol ^ !cpha;
  168. tmp |= edge << 27; /* TEDG */
  169. tmp |= edge << 26; /* REDG */
  170. tmp |= (tx_hi_z ? 2 : 0) << 22; /* TXDIZ */
  171. sh_msiof_write(p, CTR, tmp);
  172. }
  173. static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
  174. const void *tx_buf, void *rx_buf,
  175. u32 bits, u32 words)
  176. {
  177. u32 dr2 = ((bits - 1) << 24) | ((words - 1) << 16);
  178. if (tx_buf)
  179. sh_msiof_write(p, TMDR2, dr2);
  180. else
  181. sh_msiof_write(p, TMDR2, dr2 | 1);
  182. if (rx_buf)
  183. sh_msiof_write(p, RMDR2, dr2);
  184. sh_msiof_write(p, IER, STR_TEOF | STR_REOF);
  185. }
  186. static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
  187. {
  188. sh_msiof_write(p, STR, sh_msiof_read(p, STR));
  189. }
  190. static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
  191. const void *tx_buf, int words, int fs)
  192. {
  193. const u8 *buf_8 = tx_buf;
  194. int k;
  195. for (k = 0; k < words; k++)
  196. sh_msiof_write(p, TFDR, buf_8[k] << fs);
  197. }
  198. static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
  199. const void *tx_buf, int words, int fs)
  200. {
  201. const u16 *buf_16 = tx_buf;
  202. int k;
  203. for (k = 0; k < words; k++)
  204. sh_msiof_write(p, TFDR, buf_16[k] << fs);
  205. }
  206. static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
  207. const void *tx_buf, int words, int fs)
  208. {
  209. const u16 *buf_16 = tx_buf;
  210. int k;
  211. for (k = 0; k < words; k++)
  212. sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
  213. }
  214. static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
  215. const void *tx_buf, int words, int fs)
  216. {
  217. const u32 *buf_32 = tx_buf;
  218. int k;
  219. for (k = 0; k < words; k++)
  220. sh_msiof_write(p, TFDR, buf_32[k] << fs);
  221. }
  222. static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
  223. const void *tx_buf, int words, int fs)
  224. {
  225. const u32 *buf_32 = tx_buf;
  226. int k;
  227. for (k = 0; k < words; k++)
  228. sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
  229. }
  230. static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
  231. const void *tx_buf, int words, int fs)
  232. {
  233. const u32 *buf_32 = tx_buf;
  234. int k;
  235. for (k = 0; k < words; k++)
  236. sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs));
  237. }
  238. static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
  239. const void *tx_buf, int words, int fs)
  240. {
  241. const u32 *buf_32 = tx_buf;
  242. int k;
  243. for (k = 0; k < words; k++)
  244. sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs));
  245. }
  246. static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
  247. void *rx_buf, int words, int fs)
  248. {
  249. u8 *buf_8 = rx_buf;
  250. int k;
  251. for (k = 0; k < words; k++)
  252. buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
  253. }
  254. static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
  255. void *rx_buf, int words, int fs)
  256. {
  257. u16 *buf_16 = rx_buf;
  258. int k;
  259. for (k = 0; k < words; k++)
  260. buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
  261. }
  262. static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
  263. void *rx_buf, int words, int fs)
  264. {
  265. u16 *buf_16 = rx_buf;
  266. int k;
  267. for (k = 0; k < words; k++)
  268. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
  269. }
  270. static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
  271. void *rx_buf, int words, int fs)
  272. {
  273. u32 *buf_32 = rx_buf;
  274. int k;
  275. for (k = 0; k < words; k++)
  276. buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
  277. }
  278. static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
  279. void *rx_buf, int words, int fs)
  280. {
  281. u32 *buf_32 = rx_buf;
  282. int k;
  283. for (k = 0; k < words; k++)
  284. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
  285. }
  286. static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
  287. void *rx_buf, int words, int fs)
  288. {
  289. u32 *buf_32 = rx_buf;
  290. int k;
  291. for (k = 0; k < words; k++)
  292. buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs);
  293. }
  294. static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
  295. void *rx_buf, int words, int fs)
  296. {
  297. u32 *buf_32 = rx_buf;
  298. int k;
  299. for (k = 0; k < words; k++)
  300. put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]);
  301. }
  302. static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t)
  303. {
  304. int bits;
  305. bits = t ? t->bits_per_word : 0;
  306. if (!bits)
  307. bits = spi->bits_per_word;
  308. return bits;
  309. }
  310. static unsigned long sh_msiof_spi_hz(struct spi_device *spi,
  311. struct spi_transfer *t)
  312. {
  313. unsigned long hz;
  314. hz = t ? t->speed_hz : 0;
  315. if (!hz)
  316. hz = spi->max_speed_hz;
  317. return hz;
  318. }
  319. static int sh_msiof_spi_setup_transfer(struct spi_device *spi,
  320. struct spi_transfer *t)
  321. {
  322. int bits;
  323. /* noting to check hz values against since parent clock is disabled */
  324. bits = sh_msiof_spi_bits(spi, t);
  325. if (bits < 8)
  326. return -EINVAL;
  327. if (bits > 32)
  328. return -EINVAL;
  329. return spi_bitbang_setup_transfer(spi, t);
  330. }
  331. static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
  332. {
  333. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  334. int value;
  335. /* chip select is active low unless SPI_CS_HIGH is set */
  336. if (spi->mode & SPI_CS_HIGH)
  337. value = (is_on == BITBANG_CS_ACTIVE) ? 1 : 0;
  338. else
  339. value = (is_on == BITBANG_CS_ACTIVE) ? 0 : 1;
  340. if (is_on == BITBANG_CS_ACTIVE) {
  341. if (!test_and_set_bit(0, &p->flags)) {
  342. pm_runtime_get_sync(&p->pdev->dev);
  343. clk_enable(p->clk);
  344. }
  345. /* Configure pins before asserting CS */
  346. sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
  347. !!(spi->mode & SPI_CPHA),
  348. !!(spi->mode & SPI_3WIRE),
  349. !!(spi->mode & SPI_LSB_FIRST),
  350. !!(spi->mode & SPI_CS_HIGH));
  351. }
  352. /* use spi->controller data for CS (same strategy as spi_gpio) */
  353. gpio_set_value((uintptr_t)spi->controller_data, value);
  354. if (is_on == BITBANG_CS_INACTIVE) {
  355. if (test_and_clear_bit(0, &p->flags)) {
  356. clk_disable(p->clk);
  357. pm_runtime_put(&p->pdev->dev);
  358. }
  359. }
  360. }
  361. static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
  362. void (*tx_fifo)(struct sh_msiof_spi_priv *,
  363. const void *, int, int),
  364. void (*rx_fifo)(struct sh_msiof_spi_priv *,
  365. void *, int, int),
  366. const void *tx_buf, void *rx_buf,
  367. int words, int bits)
  368. {
  369. int fifo_shift;
  370. int ret;
  371. /* limit maximum word transfer to rx/tx fifo size */
  372. if (tx_buf)
  373. words = min_t(int, words, p->tx_fifo_size);
  374. if (rx_buf)
  375. words = min_t(int, words, p->rx_fifo_size);
  376. /* the fifo contents need shifting */
  377. fifo_shift = 32 - bits;
  378. /* setup msiof transfer mode registers */
  379. sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
  380. /* write tx fifo */
  381. if (tx_buf)
  382. tx_fifo(p, tx_buf, words, fifo_shift);
  383. /* setup clock and rx/tx signals */
  384. ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
  385. if (rx_buf)
  386. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
  387. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
  388. /* start by setting frame bit */
  389. reinit_completion(&p->done);
  390. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
  391. if (ret) {
  392. dev_err(&p->pdev->dev, "failed to start hardware\n");
  393. goto err;
  394. }
  395. /* wait for tx fifo to be emptied / rx fifo to be filled */
  396. wait_for_completion(&p->done);
  397. /* read rx fifo */
  398. if (rx_buf)
  399. rx_fifo(p, rx_buf, words, fifo_shift);
  400. /* clear status bits */
  401. sh_msiof_reset_str(p);
  402. /* shut down frame, tx/tx and clock signals */
  403. ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
  404. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
  405. if (rx_buf)
  406. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
  407. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
  408. if (ret) {
  409. dev_err(&p->pdev->dev, "failed to shut down hardware\n");
  410. goto err;
  411. }
  412. return words;
  413. err:
  414. sh_msiof_write(p, IER, 0);
  415. return ret;
  416. }
  417. static int sh_msiof_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  418. {
  419. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  420. void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
  421. void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
  422. int bits;
  423. int bytes_per_word;
  424. int bytes_done;
  425. int words;
  426. int n;
  427. bool swab;
  428. bits = sh_msiof_spi_bits(spi, t);
  429. if (bits <= 8 && t->len > 15 && !(t->len & 3)) {
  430. bits = 32;
  431. swab = true;
  432. } else {
  433. swab = false;
  434. }
  435. /* setup bytes per word and fifo read/write functions */
  436. if (bits <= 8) {
  437. bytes_per_word = 1;
  438. tx_fifo = sh_msiof_spi_write_fifo_8;
  439. rx_fifo = sh_msiof_spi_read_fifo_8;
  440. } else if (bits <= 16) {
  441. bytes_per_word = 2;
  442. if ((unsigned long)t->tx_buf & 0x01)
  443. tx_fifo = sh_msiof_spi_write_fifo_16u;
  444. else
  445. tx_fifo = sh_msiof_spi_write_fifo_16;
  446. if ((unsigned long)t->rx_buf & 0x01)
  447. rx_fifo = sh_msiof_spi_read_fifo_16u;
  448. else
  449. rx_fifo = sh_msiof_spi_read_fifo_16;
  450. } else if (swab) {
  451. bytes_per_word = 4;
  452. if ((unsigned long)t->tx_buf & 0x03)
  453. tx_fifo = sh_msiof_spi_write_fifo_s32u;
  454. else
  455. tx_fifo = sh_msiof_spi_write_fifo_s32;
  456. if ((unsigned long)t->rx_buf & 0x03)
  457. rx_fifo = sh_msiof_spi_read_fifo_s32u;
  458. else
  459. rx_fifo = sh_msiof_spi_read_fifo_s32;
  460. } else {
  461. bytes_per_word = 4;
  462. if ((unsigned long)t->tx_buf & 0x03)
  463. tx_fifo = sh_msiof_spi_write_fifo_32u;
  464. else
  465. tx_fifo = sh_msiof_spi_write_fifo_32;
  466. if ((unsigned long)t->rx_buf & 0x03)
  467. rx_fifo = sh_msiof_spi_read_fifo_32u;
  468. else
  469. rx_fifo = sh_msiof_spi_read_fifo_32;
  470. }
  471. /* setup clocks (clock already enabled in chipselect()) */
  472. sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk),
  473. sh_msiof_spi_hz(spi, t));
  474. /* transfer in fifo sized chunks */
  475. words = t->len / bytes_per_word;
  476. bytes_done = 0;
  477. while (bytes_done < t->len) {
  478. void *rx_buf = t->rx_buf ? t->rx_buf + bytes_done : NULL;
  479. const void *tx_buf = t->tx_buf ? t->tx_buf + bytes_done : NULL;
  480. n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo,
  481. tx_buf,
  482. rx_buf,
  483. words, bits);
  484. if (n < 0)
  485. break;
  486. bytes_done += n * bytes_per_word;
  487. words -= n;
  488. }
  489. return bytes_done;
  490. }
  491. static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
  492. u32 word, u8 bits)
  493. {
  494. BUG(); /* unused but needed by bitbang code */
  495. return 0;
  496. }
  497. #ifdef CONFIG_OF
  498. static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
  499. {
  500. struct sh_msiof_spi_info *info;
  501. struct device_node *np = dev->of_node;
  502. u32 num_cs = 0;
  503. info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL);
  504. if (!info) {
  505. dev_err(dev, "failed to allocate setup data\n");
  506. return NULL;
  507. }
  508. /* Parse the MSIOF properties */
  509. of_property_read_u32(np, "num-cs", &num_cs);
  510. of_property_read_u32(np, "renesas,tx-fifo-size",
  511. &info->tx_fifo_override);
  512. of_property_read_u32(np, "renesas,rx-fifo-size",
  513. &info->rx_fifo_override);
  514. info->num_chipselect = num_cs;
  515. return info;
  516. }
  517. #else
  518. static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
  519. {
  520. return NULL;
  521. }
  522. #endif
  523. static int sh_msiof_spi_probe(struct platform_device *pdev)
  524. {
  525. struct resource *r;
  526. struct spi_master *master;
  527. struct sh_msiof_spi_priv *p;
  528. int i;
  529. int ret;
  530. master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
  531. if (master == NULL) {
  532. dev_err(&pdev->dev, "failed to allocate spi master\n");
  533. return -ENOMEM;
  534. }
  535. p = spi_master_get_devdata(master);
  536. platform_set_drvdata(pdev, p);
  537. if (pdev->dev.of_node)
  538. p->info = sh_msiof_spi_parse_dt(&pdev->dev);
  539. else
  540. p->info = dev_get_platdata(&pdev->dev);
  541. if (!p->info) {
  542. dev_err(&pdev->dev, "failed to obtain device info\n");
  543. ret = -ENXIO;
  544. goto err1;
  545. }
  546. init_completion(&p->done);
  547. p->clk = devm_clk_get(&pdev->dev, NULL);
  548. if (IS_ERR(p->clk)) {
  549. dev_err(&pdev->dev, "cannot get clock\n");
  550. ret = PTR_ERR(p->clk);
  551. goto err1;
  552. }
  553. i = platform_get_irq(pdev, 0);
  554. if (i < 0) {
  555. dev_err(&pdev->dev, "cannot get platform IRQ\n");
  556. ret = -ENOENT;
  557. goto err1;
  558. }
  559. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  560. p->mapbase = devm_ioremap_resource(&pdev->dev, r);
  561. if (IS_ERR(p->mapbase)) {
  562. ret = PTR_ERR(p->mapbase);
  563. goto err1;
  564. }
  565. ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0,
  566. dev_name(&pdev->dev), p);
  567. if (ret) {
  568. dev_err(&pdev->dev, "unable to request irq\n");
  569. goto err1;
  570. }
  571. ret = clk_prepare(p->clk);
  572. if (ret < 0) {
  573. dev_err(&pdev->dev, "unable to prepare clock\n");
  574. goto err1;
  575. }
  576. p->pdev = pdev;
  577. pm_runtime_enable(&pdev->dev);
  578. /* The standard version of MSIOF use 64 word FIFOs */
  579. p->tx_fifo_size = 64;
  580. p->rx_fifo_size = 64;
  581. /* Platform data may override FIFO sizes */
  582. if (p->info->tx_fifo_override)
  583. p->tx_fifo_size = p->info->tx_fifo_override;
  584. if (p->info->rx_fifo_override)
  585. p->rx_fifo_size = p->info->rx_fifo_override;
  586. /* init master and bitbang code */
  587. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  588. master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
  589. master->flags = 0;
  590. master->bus_num = pdev->id;
  591. master->num_chipselect = p->info->num_chipselect;
  592. master->setup = spi_bitbang_setup;
  593. master->cleanup = spi_bitbang_cleanup;
  594. p->bitbang.master = master;
  595. p->bitbang.chipselect = sh_msiof_spi_chipselect;
  596. p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
  597. p->bitbang.txrx_bufs = sh_msiof_spi_txrx;
  598. p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word;
  599. p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word;
  600. p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word;
  601. p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word;
  602. ret = spi_bitbang_start(&p->bitbang);
  603. if (ret == 0)
  604. return 0;
  605. pm_runtime_disable(&pdev->dev);
  606. clk_unprepare(p->clk);
  607. err1:
  608. spi_master_put(master);
  609. return ret;
  610. }
  611. static int sh_msiof_spi_remove(struct platform_device *pdev)
  612. {
  613. struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
  614. int ret;
  615. ret = spi_bitbang_stop(&p->bitbang);
  616. if (!ret) {
  617. pm_runtime_disable(&pdev->dev);
  618. clk_unprepare(p->clk);
  619. spi_master_put(p->bitbang.master);
  620. }
  621. return ret;
  622. }
  623. #ifdef CONFIG_OF
  624. static const struct of_device_id sh_msiof_match[] = {
  625. { .compatible = "renesas,sh-msiof", },
  626. { .compatible = "renesas,sh-mobile-msiof", },
  627. {},
  628. };
  629. MODULE_DEVICE_TABLE(of, sh_msiof_match);
  630. #endif
  631. static struct platform_driver sh_msiof_spi_drv = {
  632. .probe = sh_msiof_spi_probe,
  633. .remove = sh_msiof_spi_remove,
  634. .driver = {
  635. .name = "spi_sh_msiof",
  636. .owner = THIS_MODULE,
  637. .of_match_table = of_match_ptr(sh_msiof_match),
  638. },
  639. };
  640. module_platform_driver(sh_msiof_spi_drv);
  641. MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
  642. MODULE_AUTHOR("Magnus Damm");
  643. MODULE_LICENSE("GPL v2");
  644. MODULE_ALIAS("platform:spi_sh_msiof");