spi-omap2-mcspi.c 37 KB

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