spi-omap2-mcspi.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /*
  2. * OMAP2 McSPI controller driver
  3. *
  4. * Copyright (C) 2005, 2006 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  6. * Juha Yrj�l� <juha.yrjola@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/omap-dma.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/err.h>
  28. #include <linux/clk.h>
  29. #include <linux/io.h>
  30. #include <linux/slab.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/gcd.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/gpio.h>
  37. #include <linux/platform_data/spi-omap2-mcspi.h>
  38. #define OMAP2_MCSPI_MAX_FREQ 48000000
  39. #define OMAP2_MCSPI_MAX_DIVIDER 4096
  40. #define OMAP2_MCSPI_MAX_FIFODEPTH 64
  41. #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
  42. #define SPI_AUTOSUSPEND_TIMEOUT 2000
  43. #define OMAP2_MCSPI_REVISION 0x00
  44. #define OMAP2_MCSPI_SYSSTATUS 0x14
  45. #define OMAP2_MCSPI_IRQSTATUS 0x18
  46. #define OMAP2_MCSPI_IRQENABLE 0x1c
  47. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  48. #define OMAP2_MCSPI_SYST 0x24
  49. #define OMAP2_MCSPI_MODULCTRL 0x28
  50. #define OMAP2_MCSPI_XFERLEVEL 0x7c
  51. /* per-channel banks, 0x14 bytes each, first is: */
  52. #define OMAP2_MCSPI_CHCONF0 0x2c
  53. #define OMAP2_MCSPI_CHSTAT0 0x30
  54. #define OMAP2_MCSPI_CHCTRL0 0x34
  55. #define OMAP2_MCSPI_TX0 0x38
  56. #define OMAP2_MCSPI_RX0 0x3c
  57. /* per-register bitmasks: */
  58. #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
  59. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  60. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  61. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  62. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  63. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  64. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  65. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  66. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  67. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  68. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  69. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  70. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  71. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  72. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  73. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  74. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  75. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  76. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  77. #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
  78. #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
  79. #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
  80. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  81. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  82. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  83. #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
  84. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  85. #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
  86. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  87. /* We have 2 DMA channels per CS, one for RX and one for TX */
  88. struct omap2_mcspi_dma {
  89. struct dma_chan *dma_tx;
  90. struct dma_chan *dma_rx;
  91. int dma_tx_sync_dev;
  92. int dma_rx_sync_dev;
  93. struct completion dma_tx_completion;
  94. struct completion dma_rx_completion;
  95. char dma_rx_ch_name[14];
  96. char dma_tx_ch_name[14];
  97. };
  98. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  99. * cache operations; better heuristics consider wordsize and bitrate.
  100. */
  101. #define DMA_MIN_BYTES 160
  102. /*
  103. * Used for context save and restore, structure members to be updated whenever
  104. * corresponding registers are modified.
  105. */
  106. struct omap2_mcspi_regs {
  107. u32 modulctrl;
  108. u32 wakeupenable;
  109. struct list_head cs;
  110. };
  111. struct omap2_mcspi {
  112. struct spi_master *master;
  113. /* Virtual base address of the controller */
  114. void __iomem *base;
  115. unsigned long phys;
  116. /* SPI1 has 4 channels, while SPI2 has 2 */
  117. struct omap2_mcspi_dma *dma_channels;
  118. struct device *dev;
  119. struct omap2_mcspi_regs ctx;
  120. int fifo_depth;
  121. unsigned int pin_dir:1;
  122. };
  123. struct omap2_mcspi_cs {
  124. void __iomem *base;
  125. unsigned long phys;
  126. int word_len;
  127. u16 mode;
  128. struct list_head node;
  129. /* Context save and restore shadow register */
  130. u32 chconf0, chctrl0;
  131. };
  132. static inline void mcspi_write_reg(struct spi_master *master,
  133. int idx, u32 val)
  134. {
  135. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  136. writel_relaxed(val, mcspi->base + idx);
  137. }
  138. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  139. {
  140. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  141. return readl_relaxed(mcspi->base + idx);
  142. }
  143. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  144. int idx, u32 val)
  145. {
  146. struct omap2_mcspi_cs *cs = spi->controller_state;
  147. writel_relaxed(val, cs->base + idx);
  148. }
  149. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  150. {
  151. struct omap2_mcspi_cs *cs = spi->controller_state;
  152. return readl_relaxed(cs->base + idx);
  153. }
  154. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  155. {
  156. struct omap2_mcspi_cs *cs = spi->controller_state;
  157. return cs->chconf0;
  158. }
  159. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  160. {
  161. struct omap2_mcspi_cs *cs = spi->controller_state;
  162. cs->chconf0 = val;
  163. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  164. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  165. }
  166. static inline int mcspi_bytes_per_word(int word_len)
  167. {
  168. if (word_len <= 8)
  169. return 1;
  170. else if (word_len <= 16)
  171. return 2;
  172. else /* word_len <= 32 */
  173. return 4;
  174. }
  175. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  176. int is_read, int enable)
  177. {
  178. u32 l, rw;
  179. l = mcspi_cached_chconf0(spi);
  180. if (is_read) /* 1 is read, 0 write */
  181. rw = OMAP2_MCSPI_CHCONF_DMAR;
  182. else
  183. rw = OMAP2_MCSPI_CHCONF_DMAW;
  184. if (enable)
  185. l |= rw;
  186. else
  187. l &= ~rw;
  188. mcspi_write_chconf0(spi, l);
  189. }
  190. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  191. {
  192. struct omap2_mcspi_cs *cs = spi->controller_state;
  193. u32 l;
  194. l = cs->chctrl0;
  195. if (enable)
  196. l |= OMAP2_MCSPI_CHCTRL_EN;
  197. else
  198. l &= ~OMAP2_MCSPI_CHCTRL_EN;
  199. cs->chctrl0 = l;
  200. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  201. /* Flash post-writes */
  202. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  203. }
  204. static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
  205. {
  206. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  207. u32 l;
  208. /* The controller handles the inverted chip selects
  209. * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
  210. * the inversion from the core spi_set_cs function.
  211. */
  212. if (spi->mode & SPI_CS_HIGH)
  213. enable = !enable;
  214. if (spi->controller_state) {
  215. int err = pm_runtime_get_sync(mcspi->dev);
  216. if (err < 0) {
  217. dev_err(mcspi->dev, "failed to get sync: %d\n", err);
  218. return;
  219. }
  220. l = mcspi_cached_chconf0(spi);
  221. if (enable)
  222. l &= ~OMAP2_MCSPI_CHCONF_FORCE;
  223. else
  224. l |= OMAP2_MCSPI_CHCONF_FORCE;
  225. mcspi_write_chconf0(spi, l);
  226. pm_runtime_mark_last_busy(mcspi->dev);
  227. pm_runtime_put_autosuspend(mcspi->dev);
  228. }
  229. }
  230. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  231. {
  232. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  233. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  234. u32 l;
  235. /*
  236. * Setup when switching from (reset default) slave mode
  237. * to single-channel master mode
  238. */
  239. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  240. l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
  241. l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  242. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  243. ctx->modulctrl = l;
  244. }
  245. static void omap2_mcspi_set_fifo(const struct spi_device *spi,
  246. struct spi_transfer *t, int enable)
  247. {
  248. struct spi_master *master = spi->master;
  249. struct omap2_mcspi_cs *cs = spi->controller_state;
  250. struct omap2_mcspi *mcspi;
  251. unsigned int wcnt;
  252. int max_fifo_depth, fifo_depth, bytes_per_word;
  253. u32 chconf, xferlevel;
  254. mcspi = spi_master_get_devdata(master);
  255. chconf = mcspi_cached_chconf0(spi);
  256. if (enable) {
  257. bytes_per_word = mcspi_bytes_per_word(cs->word_len);
  258. if (t->len % bytes_per_word != 0)
  259. goto disable_fifo;
  260. if (t->rx_buf != NULL && t->tx_buf != NULL)
  261. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
  262. else
  263. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
  264. fifo_depth = gcd(t->len, max_fifo_depth);
  265. if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
  266. goto disable_fifo;
  267. wcnt = t->len / bytes_per_word;
  268. if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
  269. goto disable_fifo;
  270. xferlevel = wcnt << 16;
  271. if (t->rx_buf != NULL) {
  272. chconf |= OMAP2_MCSPI_CHCONF_FFER;
  273. xferlevel |= (fifo_depth - 1) << 8;
  274. }
  275. if (t->tx_buf != NULL) {
  276. chconf |= OMAP2_MCSPI_CHCONF_FFET;
  277. xferlevel |= fifo_depth - 1;
  278. }
  279. mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
  280. mcspi_write_chconf0(spi, chconf);
  281. mcspi->fifo_depth = fifo_depth;
  282. return;
  283. }
  284. disable_fifo:
  285. if (t->rx_buf != NULL)
  286. chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
  287. if (t->tx_buf != NULL)
  288. chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
  289. mcspi_write_chconf0(spi, chconf);
  290. mcspi->fifo_depth = 0;
  291. }
  292. static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
  293. {
  294. struct spi_master *spi_cntrl = mcspi->master;
  295. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  296. struct omap2_mcspi_cs *cs;
  297. /* McSPI: context restore */
  298. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
  299. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
  300. list_for_each_entry(cs, &ctx->cs, node)
  301. writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  302. }
  303. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  304. {
  305. unsigned long timeout;
  306. timeout = jiffies + msecs_to_jiffies(1000);
  307. while (!(readl_relaxed(reg) & bit)) {
  308. if (time_after(jiffies, timeout)) {
  309. if (!(readl_relaxed(reg) & bit))
  310. return -ETIMEDOUT;
  311. else
  312. return 0;
  313. }
  314. cpu_relax();
  315. }
  316. return 0;
  317. }
  318. static void omap2_mcspi_rx_callback(void *data)
  319. {
  320. struct spi_device *spi = data;
  321. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  322. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  323. /* We must disable the DMA RX request */
  324. omap2_mcspi_set_dma_req(spi, 1, 0);
  325. complete(&mcspi_dma->dma_rx_completion);
  326. }
  327. static void omap2_mcspi_tx_callback(void *data)
  328. {
  329. struct spi_device *spi = data;
  330. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  331. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  332. /* We must disable the DMA TX request */
  333. omap2_mcspi_set_dma_req(spi, 0, 0);
  334. complete(&mcspi_dma->dma_tx_completion);
  335. }
  336. static void omap2_mcspi_tx_dma(struct spi_device *spi,
  337. struct spi_transfer *xfer,
  338. struct dma_slave_config cfg)
  339. {
  340. struct omap2_mcspi *mcspi;
  341. struct omap2_mcspi_dma *mcspi_dma;
  342. unsigned int count;
  343. mcspi = spi_master_get_devdata(spi->master);
  344. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  345. count = xfer->len;
  346. if (mcspi_dma->dma_tx) {
  347. struct dma_async_tx_descriptor *tx;
  348. struct scatterlist sg;
  349. dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
  350. sg_init_table(&sg, 1);
  351. sg_dma_address(&sg) = xfer->tx_dma;
  352. sg_dma_len(&sg) = xfer->len;
  353. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
  354. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  355. if (tx) {
  356. tx->callback = omap2_mcspi_tx_callback;
  357. tx->callback_param = spi;
  358. dmaengine_submit(tx);
  359. } else {
  360. /* FIXME: fall back to PIO? */
  361. }
  362. }
  363. dma_async_issue_pending(mcspi_dma->dma_tx);
  364. omap2_mcspi_set_dma_req(spi, 0, 1);
  365. }
  366. static unsigned
  367. omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
  368. struct dma_slave_config cfg,
  369. unsigned es)
  370. {
  371. struct omap2_mcspi *mcspi;
  372. struct omap2_mcspi_dma *mcspi_dma;
  373. unsigned int count, dma_count;
  374. u32 l;
  375. int elements = 0;
  376. int word_len, element_count;
  377. struct omap2_mcspi_cs *cs = spi->controller_state;
  378. mcspi = spi_master_get_devdata(spi->master);
  379. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  380. count = xfer->len;
  381. dma_count = xfer->len;
  382. if (mcspi->fifo_depth == 0)
  383. dma_count -= es;
  384. word_len = cs->word_len;
  385. l = mcspi_cached_chconf0(spi);
  386. if (word_len <= 8)
  387. element_count = count;
  388. else if (word_len <= 16)
  389. element_count = count >> 1;
  390. else /* word_len <= 32 */
  391. element_count = count >> 2;
  392. if (mcspi_dma->dma_rx) {
  393. struct dma_async_tx_descriptor *tx;
  394. struct scatterlist sg;
  395. dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
  396. if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
  397. dma_count -= es;
  398. sg_init_table(&sg, 1);
  399. sg_dma_address(&sg) = xfer->rx_dma;
  400. sg_dma_len(&sg) = dma_count;
  401. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
  402. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
  403. DMA_CTRL_ACK);
  404. if (tx) {
  405. tx->callback = omap2_mcspi_rx_callback;
  406. tx->callback_param = spi;
  407. dmaengine_submit(tx);
  408. } else {
  409. /* FIXME: fall back to PIO? */
  410. }
  411. }
  412. dma_async_issue_pending(mcspi_dma->dma_rx);
  413. omap2_mcspi_set_dma_req(spi, 1, 1);
  414. wait_for_completion(&mcspi_dma->dma_rx_completion);
  415. dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
  416. DMA_FROM_DEVICE);
  417. if (mcspi->fifo_depth > 0)
  418. return count;
  419. omap2_mcspi_set_enable(spi, 0);
  420. elements = element_count - 1;
  421. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  422. elements--;
  423. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  424. & OMAP2_MCSPI_CHSTAT_RXS)) {
  425. u32 w;
  426. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  427. if (word_len <= 8)
  428. ((u8 *)xfer->rx_buf)[elements++] = w;
  429. else if (word_len <= 16)
  430. ((u16 *)xfer->rx_buf)[elements++] = w;
  431. else /* word_len <= 32 */
  432. ((u32 *)xfer->rx_buf)[elements++] = w;
  433. } else {
  434. int bytes_per_word = mcspi_bytes_per_word(word_len);
  435. dev_err(&spi->dev, "DMA RX penultimate word empty\n");
  436. count -= (bytes_per_word << 1);
  437. omap2_mcspi_set_enable(spi, 1);
  438. return count;
  439. }
  440. }
  441. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  442. & OMAP2_MCSPI_CHSTAT_RXS)) {
  443. u32 w;
  444. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  445. if (word_len <= 8)
  446. ((u8 *)xfer->rx_buf)[elements] = w;
  447. else if (word_len <= 16)
  448. ((u16 *)xfer->rx_buf)[elements] = w;
  449. else /* word_len <= 32 */
  450. ((u32 *)xfer->rx_buf)[elements] = w;
  451. } else {
  452. dev_err(&spi->dev, "DMA RX last word empty\n");
  453. count -= mcspi_bytes_per_word(word_len);
  454. }
  455. omap2_mcspi_set_enable(spi, 1);
  456. return count;
  457. }
  458. static unsigned
  459. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  460. {
  461. struct omap2_mcspi *mcspi;
  462. struct omap2_mcspi_cs *cs = spi->controller_state;
  463. struct omap2_mcspi_dma *mcspi_dma;
  464. unsigned int count;
  465. u32 l;
  466. u8 *rx;
  467. const u8 *tx;
  468. struct dma_slave_config cfg;
  469. enum dma_slave_buswidth width;
  470. unsigned es;
  471. u32 burst;
  472. void __iomem *chstat_reg;
  473. void __iomem *irqstat_reg;
  474. int wait_res;
  475. mcspi = spi_master_get_devdata(spi->master);
  476. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  477. l = mcspi_cached_chconf0(spi);
  478. if (cs->word_len <= 8) {
  479. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  480. es = 1;
  481. } else if (cs->word_len <= 16) {
  482. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  483. es = 2;
  484. } else {
  485. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  486. es = 4;
  487. }
  488. count = xfer->len;
  489. burst = 1;
  490. if (mcspi->fifo_depth > 0) {
  491. if (count > mcspi->fifo_depth)
  492. burst = mcspi->fifo_depth / es;
  493. else
  494. burst = count / es;
  495. }
  496. memset(&cfg, 0, sizeof(cfg));
  497. cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
  498. cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
  499. cfg.src_addr_width = width;
  500. cfg.dst_addr_width = width;
  501. cfg.src_maxburst = burst;
  502. cfg.dst_maxburst = burst;
  503. rx = xfer->rx_buf;
  504. tx = xfer->tx_buf;
  505. if (tx != NULL)
  506. omap2_mcspi_tx_dma(spi, xfer, cfg);
  507. if (rx != NULL)
  508. count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
  509. if (tx != NULL) {
  510. wait_for_completion(&mcspi_dma->dma_tx_completion);
  511. dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
  512. DMA_TO_DEVICE);
  513. if (mcspi->fifo_depth > 0) {
  514. irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
  515. if (mcspi_wait_for_reg_bit(irqstat_reg,
  516. OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
  517. dev_err(&spi->dev, "EOW timed out\n");
  518. mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
  519. OMAP2_MCSPI_IRQSTATUS_EOW);
  520. }
  521. /* for TX_ONLY mode, be sure all words have shifted out */
  522. if (rx == NULL) {
  523. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  524. if (mcspi->fifo_depth > 0) {
  525. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  526. OMAP2_MCSPI_CHSTAT_TXFFE);
  527. if (wait_res < 0)
  528. dev_err(&spi->dev, "TXFFE timed out\n");
  529. } else {
  530. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  531. OMAP2_MCSPI_CHSTAT_TXS);
  532. if (wait_res < 0)
  533. dev_err(&spi->dev, "TXS timed out\n");
  534. }
  535. if (wait_res >= 0 &&
  536. (mcspi_wait_for_reg_bit(chstat_reg,
  537. OMAP2_MCSPI_CHSTAT_EOT) < 0))
  538. dev_err(&spi->dev, "EOT timed out\n");
  539. }
  540. }
  541. return count;
  542. }
  543. static unsigned
  544. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  545. {
  546. struct omap2_mcspi *mcspi;
  547. struct omap2_mcspi_cs *cs = spi->controller_state;
  548. unsigned int count, c;
  549. u32 l;
  550. void __iomem *base = cs->base;
  551. void __iomem *tx_reg;
  552. void __iomem *rx_reg;
  553. void __iomem *chstat_reg;
  554. int word_len;
  555. mcspi = spi_master_get_devdata(spi->master);
  556. count = xfer->len;
  557. c = count;
  558. word_len = cs->word_len;
  559. l = mcspi_cached_chconf0(spi);
  560. /* We store the pre-calculated register addresses on stack to speed
  561. * up the transfer loop. */
  562. tx_reg = base + OMAP2_MCSPI_TX0;
  563. rx_reg = base + OMAP2_MCSPI_RX0;
  564. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  565. if (c < (word_len>>3))
  566. return 0;
  567. if (word_len <= 8) {
  568. u8 *rx;
  569. const u8 *tx;
  570. rx = xfer->rx_buf;
  571. tx = xfer->tx_buf;
  572. do {
  573. c -= 1;
  574. if (tx != NULL) {
  575. if (mcspi_wait_for_reg_bit(chstat_reg,
  576. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  577. dev_err(&spi->dev, "TXS timed out\n");
  578. goto out;
  579. }
  580. dev_vdbg(&spi->dev, "write-%d %02x\n",
  581. word_len, *tx);
  582. writel_relaxed(*tx++, tx_reg);
  583. }
  584. if (rx != NULL) {
  585. if (mcspi_wait_for_reg_bit(chstat_reg,
  586. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  587. dev_err(&spi->dev, "RXS timed out\n");
  588. goto out;
  589. }
  590. if (c == 1 && tx == NULL &&
  591. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  592. omap2_mcspi_set_enable(spi, 0);
  593. *rx++ = readl_relaxed(rx_reg);
  594. dev_vdbg(&spi->dev, "read-%d %02x\n",
  595. word_len, *(rx - 1));
  596. if (mcspi_wait_for_reg_bit(chstat_reg,
  597. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  598. dev_err(&spi->dev,
  599. "RXS timed out\n");
  600. goto out;
  601. }
  602. c = 0;
  603. } else if (c == 0 && tx == NULL) {
  604. omap2_mcspi_set_enable(spi, 0);
  605. }
  606. *rx++ = readl_relaxed(rx_reg);
  607. dev_vdbg(&spi->dev, "read-%d %02x\n",
  608. word_len, *(rx - 1));
  609. }
  610. } while (c);
  611. } else if (word_len <= 16) {
  612. u16 *rx;
  613. const u16 *tx;
  614. rx = xfer->rx_buf;
  615. tx = xfer->tx_buf;
  616. do {
  617. c -= 2;
  618. if (tx != NULL) {
  619. if (mcspi_wait_for_reg_bit(chstat_reg,
  620. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  621. dev_err(&spi->dev, "TXS timed out\n");
  622. goto out;
  623. }
  624. dev_vdbg(&spi->dev, "write-%d %04x\n",
  625. word_len, *tx);
  626. writel_relaxed(*tx++, tx_reg);
  627. }
  628. if (rx != NULL) {
  629. if (mcspi_wait_for_reg_bit(chstat_reg,
  630. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  631. dev_err(&spi->dev, "RXS timed out\n");
  632. goto out;
  633. }
  634. if (c == 2 && tx == NULL &&
  635. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  636. omap2_mcspi_set_enable(spi, 0);
  637. *rx++ = readl_relaxed(rx_reg);
  638. dev_vdbg(&spi->dev, "read-%d %04x\n",
  639. word_len, *(rx - 1));
  640. if (mcspi_wait_for_reg_bit(chstat_reg,
  641. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  642. dev_err(&spi->dev,
  643. "RXS timed out\n");
  644. goto out;
  645. }
  646. c = 0;
  647. } else if (c == 0 && tx == NULL) {
  648. omap2_mcspi_set_enable(spi, 0);
  649. }
  650. *rx++ = readl_relaxed(rx_reg);
  651. dev_vdbg(&spi->dev, "read-%d %04x\n",
  652. word_len, *(rx - 1));
  653. }
  654. } while (c >= 2);
  655. } else if (word_len <= 32) {
  656. u32 *rx;
  657. const u32 *tx;
  658. rx = xfer->rx_buf;
  659. tx = xfer->tx_buf;
  660. do {
  661. c -= 4;
  662. if (tx != NULL) {
  663. if (mcspi_wait_for_reg_bit(chstat_reg,
  664. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  665. dev_err(&spi->dev, "TXS timed out\n");
  666. goto out;
  667. }
  668. dev_vdbg(&spi->dev, "write-%d %08x\n",
  669. word_len, *tx);
  670. writel_relaxed(*tx++, tx_reg);
  671. }
  672. if (rx != NULL) {
  673. if (mcspi_wait_for_reg_bit(chstat_reg,
  674. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  675. dev_err(&spi->dev, "RXS timed out\n");
  676. goto out;
  677. }
  678. if (c == 4 && tx == NULL &&
  679. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  680. omap2_mcspi_set_enable(spi, 0);
  681. *rx++ = readl_relaxed(rx_reg);
  682. dev_vdbg(&spi->dev, "read-%d %08x\n",
  683. word_len, *(rx - 1));
  684. if (mcspi_wait_for_reg_bit(chstat_reg,
  685. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  686. dev_err(&spi->dev,
  687. "RXS timed out\n");
  688. goto out;
  689. }
  690. c = 0;
  691. } else if (c == 0 && tx == NULL) {
  692. omap2_mcspi_set_enable(spi, 0);
  693. }
  694. *rx++ = readl_relaxed(rx_reg);
  695. dev_vdbg(&spi->dev, "read-%d %08x\n",
  696. word_len, *(rx - 1));
  697. }
  698. } while (c >= 4);
  699. }
  700. /* for TX_ONLY mode, be sure all words have shifted out */
  701. if (xfer->rx_buf == NULL) {
  702. if (mcspi_wait_for_reg_bit(chstat_reg,
  703. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  704. dev_err(&spi->dev, "TXS timed out\n");
  705. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  706. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  707. dev_err(&spi->dev, "EOT timed out\n");
  708. /* disable chan to purge rx datas received in TX_ONLY transfer,
  709. * otherwise these rx datas will affect the direct following
  710. * RX_ONLY transfer.
  711. */
  712. omap2_mcspi_set_enable(spi, 0);
  713. }
  714. out:
  715. omap2_mcspi_set_enable(spi, 1);
  716. return count - c;
  717. }
  718. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  719. {
  720. u32 div;
  721. for (div = 0; div < 15; div++)
  722. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  723. return div;
  724. return 15;
  725. }
  726. /* called only when no transfer is active to this device */
  727. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  728. struct spi_transfer *t)
  729. {
  730. struct omap2_mcspi_cs *cs = spi->controller_state;
  731. struct omap2_mcspi *mcspi;
  732. struct spi_master *spi_cntrl;
  733. u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
  734. u8 word_len = spi->bits_per_word;
  735. u32 speed_hz = spi->max_speed_hz;
  736. mcspi = spi_master_get_devdata(spi->master);
  737. spi_cntrl = mcspi->master;
  738. if (t != NULL && t->bits_per_word)
  739. word_len = t->bits_per_word;
  740. cs->word_len = word_len;
  741. if (t && t->speed_hz)
  742. speed_hz = t->speed_hz;
  743. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  744. if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
  745. clkd = omap2_mcspi_calc_divisor(speed_hz);
  746. speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
  747. clkg = 0;
  748. } else {
  749. div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
  750. speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
  751. clkd = (div - 1) & 0xf;
  752. extclk = (div - 1) >> 4;
  753. clkg = OMAP2_MCSPI_CHCONF_CLKG;
  754. }
  755. l = mcspi_cached_chconf0(spi);
  756. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  757. * REVISIT: this controller could support SPI_3WIRE mode.
  758. */
  759. if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
  760. l &= ~OMAP2_MCSPI_CHCONF_IS;
  761. l &= ~OMAP2_MCSPI_CHCONF_DPE1;
  762. l |= OMAP2_MCSPI_CHCONF_DPE0;
  763. } else {
  764. l |= OMAP2_MCSPI_CHCONF_IS;
  765. l |= OMAP2_MCSPI_CHCONF_DPE1;
  766. l &= ~OMAP2_MCSPI_CHCONF_DPE0;
  767. }
  768. /* wordlength */
  769. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  770. l |= (word_len - 1) << 7;
  771. /* set chipselect polarity; manage with FORCE */
  772. if (!(spi->mode & SPI_CS_HIGH))
  773. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  774. else
  775. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  776. /* set clock divisor */
  777. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  778. l |= clkd << 2;
  779. /* set clock granularity */
  780. l &= ~OMAP2_MCSPI_CHCONF_CLKG;
  781. l |= clkg;
  782. if (clkg) {
  783. cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
  784. cs->chctrl0 |= extclk << 8;
  785. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  786. }
  787. /* set SPI mode 0..3 */
  788. if (spi->mode & SPI_CPOL)
  789. l |= OMAP2_MCSPI_CHCONF_POL;
  790. else
  791. l &= ~OMAP2_MCSPI_CHCONF_POL;
  792. if (spi->mode & SPI_CPHA)
  793. l |= OMAP2_MCSPI_CHCONF_PHA;
  794. else
  795. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  796. mcspi_write_chconf0(spi, l);
  797. cs->mode = spi->mode;
  798. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  799. speed_hz,
  800. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  801. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  802. return 0;
  803. }
  804. /*
  805. * Note that we currently allow DMA only if we get a channel
  806. * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
  807. */
  808. static int omap2_mcspi_request_dma(struct spi_device *spi)
  809. {
  810. struct spi_master *master = spi->master;
  811. struct omap2_mcspi *mcspi;
  812. struct omap2_mcspi_dma *mcspi_dma;
  813. dma_cap_mask_t mask;
  814. unsigned sig;
  815. mcspi = spi_master_get_devdata(master);
  816. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  817. init_completion(&mcspi_dma->dma_rx_completion);
  818. init_completion(&mcspi_dma->dma_tx_completion);
  819. dma_cap_zero(mask);
  820. dma_cap_set(DMA_SLAVE, mask);
  821. sig = mcspi_dma->dma_rx_sync_dev;
  822. mcspi_dma->dma_rx =
  823. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  824. &sig, &master->dev,
  825. mcspi_dma->dma_rx_ch_name);
  826. if (!mcspi_dma->dma_rx)
  827. goto no_dma;
  828. sig = mcspi_dma->dma_tx_sync_dev;
  829. mcspi_dma->dma_tx =
  830. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  831. &sig, &master->dev,
  832. mcspi_dma->dma_tx_ch_name);
  833. if (!mcspi_dma->dma_tx) {
  834. dma_release_channel(mcspi_dma->dma_rx);
  835. mcspi_dma->dma_rx = NULL;
  836. goto no_dma;
  837. }
  838. return 0;
  839. no_dma:
  840. dev_warn(&spi->dev, "not using DMA for McSPI\n");
  841. return -EAGAIN;
  842. }
  843. static int omap2_mcspi_setup(struct spi_device *spi)
  844. {
  845. int ret;
  846. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  847. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  848. struct omap2_mcspi_dma *mcspi_dma;
  849. struct omap2_mcspi_cs *cs = spi->controller_state;
  850. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  851. if (!cs) {
  852. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  853. if (!cs)
  854. return -ENOMEM;
  855. cs->base = mcspi->base + spi->chip_select * 0x14;
  856. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  857. cs->mode = 0;
  858. cs->chconf0 = 0;
  859. cs->chctrl0 = 0;
  860. spi->controller_state = cs;
  861. /* Link this to context save list */
  862. list_add_tail(&cs->node, &ctx->cs);
  863. }
  864. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
  865. ret = omap2_mcspi_request_dma(spi);
  866. if (ret < 0 && ret != -EAGAIN)
  867. return ret;
  868. }
  869. if (gpio_is_valid(spi->cs_gpio)) {
  870. ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
  871. if (ret) {
  872. dev_err(&spi->dev, "failed to request gpio\n");
  873. return ret;
  874. }
  875. gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  876. }
  877. ret = pm_runtime_get_sync(mcspi->dev);
  878. if (ret < 0)
  879. return ret;
  880. ret = omap2_mcspi_setup_transfer(spi, NULL);
  881. pm_runtime_mark_last_busy(mcspi->dev);
  882. pm_runtime_put_autosuspend(mcspi->dev);
  883. return ret;
  884. }
  885. static void omap2_mcspi_cleanup(struct spi_device *spi)
  886. {
  887. struct omap2_mcspi *mcspi;
  888. struct omap2_mcspi_dma *mcspi_dma;
  889. struct omap2_mcspi_cs *cs;
  890. mcspi = spi_master_get_devdata(spi->master);
  891. if (spi->controller_state) {
  892. /* Unlink controller state from context save list */
  893. cs = spi->controller_state;
  894. list_del(&cs->node);
  895. kfree(cs);
  896. }
  897. if (spi->chip_select < spi->master->num_chipselect) {
  898. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  899. if (mcspi_dma->dma_rx) {
  900. dma_release_channel(mcspi_dma->dma_rx);
  901. mcspi_dma->dma_rx = NULL;
  902. }
  903. if (mcspi_dma->dma_tx) {
  904. dma_release_channel(mcspi_dma->dma_tx);
  905. mcspi_dma->dma_tx = NULL;
  906. }
  907. }
  908. if (gpio_is_valid(spi->cs_gpio))
  909. gpio_free(spi->cs_gpio);
  910. }
  911. static int omap2_mcspi_work_one(struct omap2_mcspi *mcspi,
  912. struct spi_device *spi, struct spi_transfer *t)
  913. {
  914. /* We only enable one channel at a time -- the one whose message is
  915. * -- although this controller would gladly
  916. * arbitrate among multiple channels. This corresponds to "single
  917. * channel" master mode. As a side effect, we need to manage the
  918. * chipselect with the FORCE bit ... CS != channel enable.
  919. */
  920. struct spi_master *master;
  921. struct omap2_mcspi_dma *mcspi_dma;
  922. struct omap2_mcspi_cs *cs;
  923. struct omap2_mcspi_device_config *cd;
  924. int par_override = 0;
  925. int status = 0;
  926. u32 chconf;
  927. master = spi->master;
  928. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  929. cs = spi->controller_state;
  930. cd = spi->controller_data;
  931. /*
  932. * The slave driver could have changed spi->mode in which case
  933. * it will be different from cs->mode (the current hardware setup).
  934. * If so, set par_override (even though its not a parity issue) so
  935. * omap2_mcspi_setup_transfer will be called to configure the hardware
  936. * with the correct mode on the first iteration of the loop below.
  937. */
  938. if (spi->mode != cs->mode)
  939. par_override = 1;
  940. omap2_mcspi_set_enable(spi, 0);
  941. if (gpio_is_valid(spi->cs_gpio))
  942. omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
  943. if (par_override ||
  944. (t->speed_hz != spi->max_speed_hz) ||
  945. (t->bits_per_word != spi->bits_per_word)) {
  946. par_override = 1;
  947. status = omap2_mcspi_setup_transfer(spi, t);
  948. if (status < 0)
  949. goto out;
  950. if (t->speed_hz == spi->max_speed_hz &&
  951. t->bits_per_word == spi->bits_per_word)
  952. par_override = 0;
  953. }
  954. if (cd && cd->cs_per_word) {
  955. chconf = mcspi->ctx.modulctrl;
  956. chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
  957. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  958. mcspi->ctx.modulctrl =
  959. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  960. }
  961. chconf = mcspi_cached_chconf0(spi);
  962. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  963. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  964. if (t->tx_buf == NULL)
  965. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  966. else if (t->rx_buf == NULL)
  967. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  968. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  969. /* Turbo mode is for more than one word */
  970. if (t->len > ((cs->word_len + 7) >> 3))
  971. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  972. }
  973. mcspi_write_chconf0(spi, chconf);
  974. if (t->len) {
  975. unsigned count;
  976. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  977. (t->len >= DMA_MIN_BYTES))
  978. omap2_mcspi_set_fifo(spi, t, 1);
  979. omap2_mcspi_set_enable(spi, 1);
  980. /* RX_ONLY mode needs dummy data in TX reg */
  981. if (t->tx_buf == NULL)
  982. writel_relaxed(0, cs->base
  983. + OMAP2_MCSPI_TX0);
  984. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  985. (t->len >= DMA_MIN_BYTES))
  986. count = omap2_mcspi_txrx_dma(spi, t);
  987. else
  988. count = omap2_mcspi_txrx_pio(spi, t);
  989. if (count != t->len) {
  990. status = -EIO;
  991. goto out;
  992. }
  993. }
  994. omap2_mcspi_set_enable(spi, 0);
  995. if (mcspi->fifo_depth > 0)
  996. omap2_mcspi_set_fifo(spi, t, 0);
  997. out:
  998. /* Restore defaults if they were overriden */
  999. if (par_override) {
  1000. par_override = 0;
  1001. status = omap2_mcspi_setup_transfer(spi, NULL);
  1002. }
  1003. if (cd && cd->cs_per_word) {
  1004. chconf = mcspi->ctx.modulctrl;
  1005. chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  1006. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  1007. mcspi->ctx.modulctrl =
  1008. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  1009. }
  1010. omap2_mcspi_set_enable(spi, 0);
  1011. if (gpio_is_valid(spi->cs_gpio))
  1012. omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
  1013. if (mcspi->fifo_depth > 0 && t)
  1014. omap2_mcspi_set_fifo(spi, t, 0);
  1015. return status;
  1016. }
  1017. static int omap2_mcspi_transfer_one(struct spi_master *master,
  1018. struct spi_device *spi, struct spi_transfer *t)
  1019. {
  1020. struct omap2_mcspi *mcspi;
  1021. struct omap2_mcspi_dma *mcspi_dma;
  1022. const void *tx_buf = t->tx_buf;
  1023. void *rx_buf = t->rx_buf;
  1024. unsigned len = t->len;
  1025. mcspi = spi_master_get_devdata(master);
  1026. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  1027. if ((len && !(rx_buf || tx_buf))) {
  1028. dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  1029. t->speed_hz,
  1030. len,
  1031. tx_buf ? "tx" : "",
  1032. rx_buf ? "rx" : "",
  1033. t->bits_per_word);
  1034. return -EINVAL;
  1035. }
  1036. if (len < DMA_MIN_BYTES)
  1037. goto skip_dma_map;
  1038. if (mcspi_dma->dma_tx && tx_buf != NULL) {
  1039. t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
  1040. len, DMA_TO_DEVICE);
  1041. if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
  1042. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  1043. 'T', len);
  1044. return -EINVAL;
  1045. }
  1046. }
  1047. if (mcspi_dma->dma_rx && rx_buf != NULL) {
  1048. t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
  1049. DMA_FROM_DEVICE);
  1050. if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
  1051. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  1052. 'R', len);
  1053. if (tx_buf != NULL)
  1054. dma_unmap_single(mcspi->dev, t->tx_dma,
  1055. len, DMA_TO_DEVICE);
  1056. return -EINVAL;
  1057. }
  1058. }
  1059. skip_dma_map:
  1060. return omap2_mcspi_work_one(mcspi, spi, t);
  1061. }
  1062. static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  1063. {
  1064. struct spi_master *master = mcspi->master;
  1065. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1066. int ret = 0;
  1067. ret = pm_runtime_get_sync(mcspi->dev);
  1068. if (ret < 0)
  1069. return ret;
  1070. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  1071. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  1072. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  1073. omap2_mcspi_set_master_mode(master);
  1074. pm_runtime_mark_last_busy(mcspi->dev);
  1075. pm_runtime_put_autosuspend(mcspi->dev);
  1076. return 0;
  1077. }
  1078. static int omap_mcspi_runtime_resume(struct device *dev)
  1079. {
  1080. struct omap2_mcspi *mcspi;
  1081. struct spi_master *master;
  1082. master = dev_get_drvdata(dev);
  1083. mcspi = spi_master_get_devdata(master);
  1084. omap2_mcspi_restore_ctx(mcspi);
  1085. return 0;
  1086. }
  1087. static struct omap2_mcspi_platform_config omap2_pdata = {
  1088. .regs_offset = 0,
  1089. };
  1090. static struct omap2_mcspi_platform_config omap4_pdata = {
  1091. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  1092. };
  1093. static const struct of_device_id omap_mcspi_of_match[] = {
  1094. {
  1095. .compatible = "ti,omap2-mcspi",
  1096. .data = &omap2_pdata,
  1097. },
  1098. {
  1099. .compatible = "ti,omap4-mcspi",
  1100. .data = &omap4_pdata,
  1101. },
  1102. { },
  1103. };
  1104. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  1105. static int omap2_mcspi_probe(struct platform_device *pdev)
  1106. {
  1107. struct spi_master *master;
  1108. const struct omap2_mcspi_platform_config *pdata;
  1109. struct omap2_mcspi *mcspi;
  1110. struct resource *r;
  1111. int status = 0, i;
  1112. u32 regs_offset = 0;
  1113. static int bus_num = 1;
  1114. struct device_node *node = pdev->dev.of_node;
  1115. const struct of_device_id *match;
  1116. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  1117. if (master == NULL) {
  1118. dev_dbg(&pdev->dev, "master allocation failed\n");
  1119. return -ENOMEM;
  1120. }
  1121. /* the spi->mode bits understood by this driver: */
  1122. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1123. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1124. master->setup = omap2_mcspi_setup;
  1125. master->auto_runtime_pm = true;
  1126. master->transfer_one = omap2_mcspi_transfer_one;
  1127. master->set_cs = omap2_mcspi_set_cs;
  1128. master->cleanup = omap2_mcspi_cleanup;
  1129. master->dev.of_node = node;
  1130. master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
  1131. master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
  1132. platform_set_drvdata(pdev, master);
  1133. mcspi = spi_master_get_devdata(master);
  1134. mcspi->master = master;
  1135. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  1136. if (match) {
  1137. u32 num_cs = 1; /* default number of chipselect */
  1138. pdata = match->data;
  1139. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  1140. master->num_chipselect = num_cs;
  1141. master->bus_num = bus_num++;
  1142. if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
  1143. mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
  1144. } else {
  1145. pdata = dev_get_platdata(&pdev->dev);
  1146. master->num_chipselect = pdata->num_cs;
  1147. if (pdev->id != -1)
  1148. master->bus_num = pdev->id;
  1149. mcspi->pin_dir = pdata->pin_dir;
  1150. }
  1151. regs_offset = pdata->regs_offset;
  1152. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1153. if (r == NULL) {
  1154. status = -ENODEV;
  1155. goto free_master;
  1156. }
  1157. r->start += regs_offset;
  1158. r->end += regs_offset;
  1159. mcspi->phys = r->start;
  1160. mcspi->base = devm_ioremap_resource(&pdev->dev, r);
  1161. if (IS_ERR(mcspi->base)) {
  1162. status = PTR_ERR(mcspi->base);
  1163. goto free_master;
  1164. }
  1165. mcspi->dev = &pdev->dev;
  1166. INIT_LIST_HEAD(&mcspi->ctx.cs);
  1167. mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
  1168. sizeof(struct omap2_mcspi_dma),
  1169. GFP_KERNEL);
  1170. if (mcspi->dma_channels == NULL) {
  1171. status = -ENOMEM;
  1172. goto free_master;
  1173. }
  1174. for (i = 0; i < master->num_chipselect; i++) {
  1175. char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name;
  1176. char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name;
  1177. struct resource *dma_res;
  1178. sprintf(dma_rx_ch_name, "rx%d", i);
  1179. if (!pdev->dev.of_node) {
  1180. dma_res =
  1181. platform_get_resource_byname(pdev,
  1182. IORESOURCE_DMA,
  1183. dma_rx_ch_name);
  1184. if (!dma_res) {
  1185. dev_dbg(&pdev->dev,
  1186. "cannot get DMA RX channel\n");
  1187. status = -ENODEV;
  1188. break;
  1189. }
  1190. mcspi->dma_channels[i].dma_rx_sync_dev =
  1191. dma_res->start;
  1192. }
  1193. sprintf(dma_tx_ch_name, "tx%d", i);
  1194. if (!pdev->dev.of_node) {
  1195. dma_res =
  1196. platform_get_resource_byname(pdev,
  1197. IORESOURCE_DMA,
  1198. dma_tx_ch_name);
  1199. if (!dma_res) {
  1200. dev_dbg(&pdev->dev,
  1201. "cannot get DMA TX channel\n");
  1202. status = -ENODEV;
  1203. break;
  1204. }
  1205. mcspi->dma_channels[i].dma_tx_sync_dev =
  1206. dma_res->start;
  1207. }
  1208. }
  1209. if (status < 0)
  1210. goto free_master;
  1211. pm_runtime_use_autosuspend(&pdev->dev);
  1212. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1213. pm_runtime_enable(&pdev->dev);
  1214. status = omap2_mcspi_master_setup(mcspi);
  1215. if (status < 0)
  1216. goto disable_pm;
  1217. status = devm_spi_register_master(&pdev->dev, master);
  1218. if (status < 0)
  1219. goto disable_pm;
  1220. return status;
  1221. disable_pm:
  1222. pm_runtime_disable(&pdev->dev);
  1223. free_master:
  1224. spi_master_put(master);
  1225. return status;
  1226. }
  1227. static int omap2_mcspi_remove(struct platform_device *pdev)
  1228. {
  1229. struct spi_master *master = platform_get_drvdata(pdev);
  1230. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1231. pm_runtime_put_sync(mcspi->dev);
  1232. pm_runtime_disable(&pdev->dev);
  1233. return 0;
  1234. }
  1235. /* work with hotplug and coldplug */
  1236. MODULE_ALIAS("platform:omap2_mcspi");
  1237. #ifdef CONFIG_SUSPEND
  1238. /*
  1239. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1240. * unactive state when driver was suspend, then force it to unactive state at
  1241. * wake up.
  1242. */
  1243. static int omap2_mcspi_resume(struct device *dev)
  1244. {
  1245. struct spi_master *master = dev_get_drvdata(dev);
  1246. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1247. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1248. struct omap2_mcspi_cs *cs;
  1249. pm_runtime_get_sync(mcspi->dev);
  1250. list_for_each_entry(cs, &ctx->cs, node) {
  1251. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1252. /*
  1253. * We need to toggle CS state for OMAP take this
  1254. * change in account.
  1255. */
  1256. cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
  1257. writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1258. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1259. writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1260. }
  1261. }
  1262. pm_runtime_mark_last_busy(mcspi->dev);
  1263. pm_runtime_put_autosuspend(mcspi->dev);
  1264. return 0;
  1265. }
  1266. #else
  1267. #define omap2_mcspi_resume NULL
  1268. #endif
  1269. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1270. .resume = omap2_mcspi_resume,
  1271. .runtime_resume = omap_mcspi_runtime_resume,
  1272. };
  1273. static struct platform_driver omap2_mcspi_driver = {
  1274. .driver = {
  1275. .name = "omap2_mcspi",
  1276. .pm = &omap2_mcspi_pm_ops,
  1277. .of_match_table = omap_mcspi_of_match,
  1278. },
  1279. .probe = omap2_mcspi_probe,
  1280. .remove = omap2_mcspi_remove,
  1281. };
  1282. module_platform_driver(omap2_mcspi_driver);
  1283. MODULE_LICENSE("GPL");