spi-omap2-mcspi.c 38 KB

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