spi-sh-msiof.c 17 KB

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