omap2_mcspi.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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/platform_device.h>
  31. #include <linux/err.h>
  32. #include <linux/clk.h>
  33. #include <linux/io.h>
  34. #include <linux/spi/spi.h>
  35. #include <asm/arch/dma.h>
  36. #include <asm/arch/clock.h>
  37. #define OMAP2_MCSPI_MAX_FREQ 48000000
  38. #define OMAP2_MCSPI_REVISION 0x00
  39. #define OMAP2_MCSPI_SYSCONFIG 0x10
  40. #define OMAP2_MCSPI_SYSSTATUS 0x14
  41. #define OMAP2_MCSPI_IRQSTATUS 0x18
  42. #define OMAP2_MCSPI_IRQENABLE 0x1c
  43. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  44. #define OMAP2_MCSPI_SYST 0x24
  45. #define OMAP2_MCSPI_MODULCTRL 0x28
  46. /* per-channel banks, 0x14 bytes each, first is: */
  47. #define OMAP2_MCSPI_CHCONF0 0x2c
  48. #define OMAP2_MCSPI_CHSTAT0 0x30
  49. #define OMAP2_MCSPI_CHCTRL0 0x34
  50. #define OMAP2_MCSPI_TX0 0x38
  51. #define OMAP2_MCSPI_RX0 0x3c
  52. /* per-register bitmasks: */
  53. #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE (1 << 0)
  54. #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET (1 << 1)
  55. #define OMAP2_MCSPI_SYSSTATUS_RESETDONE (1 << 0)
  56. #define OMAP2_MCSPI_MODULCTRL_SINGLE (1 << 0)
  57. #define OMAP2_MCSPI_MODULCTRL_MS (1 << 2)
  58. #define OMAP2_MCSPI_MODULCTRL_STEST (1 << 3)
  59. #define OMAP2_MCSPI_CHCONF_PHA (1 << 0)
  60. #define OMAP2_MCSPI_CHCONF_POL (1 << 1)
  61. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  62. #define OMAP2_MCSPI_CHCONF_EPOL (1 << 6)
  63. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  64. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY (0x01 << 12)
  65. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY (0x02 << 12)
  66. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  67. #define OMAP2_MCSPI_CHCONF_DMAW (1 << 14)
  68. #define OMAP2_MCSPI_CHCONF_DMAR (1 << 15)
  69. #define OMAP2_MCSPI_CHCONF_DPE0 (1 << 16)
  70. #define OMAP2_MCSPI_CHCONF_DPE1 (1 << 17)
  71. #define OMAP2_MCSPI_CHCONF_IS (1 << 18)
  72. #define OMAP2_MCSPI_CHCONF_TURBO (1 << 19)
  73. #define OMAP2_MCSPI_CHCONF_FORCE (1 << 20)
  74. #define OMAP2_MCSPI_CHSTAT_RXS (1 << 0)
  75. #define OMAP2_MCSPI_CHSTAT_TXS (1 << 1)
  76. #define OMAP2_MCSPI_CHSTAT_EOT (1 << 2)
  77. #define OMAP2_MCSPI_CHCTRL_EN (1 << 0)
  78. /* We have 2 DMA channels per CS, one for RX and one for TX */
  79. struct omap2_mcspi_dma {
  80. int dma_tx_channel;
  81. int dma_rx_channel;
  82. int dma_tx_sync_dev;
  83. int dma_rx_sync_dev;
  84. struct completion dma_tx_completion;
  85. struct completion dma_rx_completion;
  86. };
  87. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  88. * cache operations; better heuristics consider wordsize and bitrate.
  89. */
  90. #define DMA_MIN_BYTES 8
  91. struct omap2_mcspi {
  92. struct work_struct work;
  93. /* lock protects queue and registers */
  94. spinlock_t lock;
  95. struct list_head msg_queue;
  96. struct spi_master *master;
  97. struct clk *ick;
  98. struct clk *fck;
  99. /* Virtual base address of the controller */
  100. void __iomem *base;
  101. /* SPI1 has 4 channels, while SPI2 has 2 */
  102. struct omap2_mcspi_dma *dma_channels;
  103. };
  104. struct omap2_mcspi_cs {
  105. void __iomem *base;
  106. int word_len;
  107. };
  108. static struct workqueue_struct *omap2_mcspi_wq;
  109. #define MOD_REG_BIT(val, mask, set) do { \
  110. if (set) \
  111. val |= mask; \
  112. else \
  113. val &= ~mask; \
  114. } while (0)
  115. static inline void mcspi_write_reg(struct spi_master *master,
  116. int idx, u32 val)
  117. {
  118. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  119. __raw_writel(val, mcspi->base + idx);
  120. }
  121. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  122. {
  123. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  124. return __raw_readl(mcspi->base + idx);
  125. }
  126. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  127. int idx, u32 val)
  128. {
  129. struct omap2_mcspi_cs *cs = spi->controller_state;
  130. __raw_writel(val, cs->base + idx);
  131. }
  132. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  133. {
  134. struct omap2_mcspi_cs *cs = spi->controller_state;
  135. return __raw_readl(cs->base + idx);
  136. }
  137. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  138. int is_read, int enable)
  139. {
  140. u32 l, rw;
  141. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  142. if (is_read) /* 1 is read, 0 write */
  143. rw = OMAP2_MCSPI_CHCONF_DMAR;
  144. else
  145. rw = OMAP2_MCSPI_CHCONF_DMAW;
  146. MOD_REG_BIT(l, rw, enable);
  147. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  148. }
  149. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  150. {
  151. u32 l;
  152. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  153. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  154. }
  155. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  156. {
  157. u32 l;
  158. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  159. MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
  160. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  161. }
  162. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  163. {
  164. u32 l;
  165. /* setup when switching from (reset default) slave mode
  166. * to single-channel master mode
  167. */
  168. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  169. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
  170. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
  171. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
  172. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  173. }
  174. static unsigned
  175. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  176. {
  177. struct omap2_mcspi *mcspi;
  178. struct omap2_mcspi_cs *cs = spi->controller_state;
  179. struct omap2_mcspi_dma *mcspi_dma;
  180. unsigned int count, c;
  181. unsigned long base, tx_reg, rx_reg;
  182. int word_len, data_type, element_count;
  183. u8 * rx;
  184. const u8 * tx;
  185. mcspi = spi_master_get_devdata(spi->master);
  186. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  187. count = xfer->len;
  188. c = count;
  189. word_len = cs->word_len;
  190. base = (unsigned long) io_v2p(cs->base);
  191. tx_reg = base + OMAP2_MCSPI_TX0;
  192. rx_reg = base + OMAP2_MCSPI_RX0;
  193. rx = xfer->rx_buf;
  194. tx = xfer->tx_buf;
  195. if (word_len <= 8) {
  196. data_type = OMAP_DMA_DATA_TYPE_S8;
  197. element_count = count;
  198. } else if (word_len <= 16) {
  199. data_type = OMAP_DMA_DATA_TYPE_S16;
  200. element_count = count >> 1;
  201. } else /* word_len <= 32 */ {
  202. data_type = OMAP_DMA_DATA_TYPE_S32;
  203. element_count = count >> 2;
  204. }
  205. if (tx != NULL) {
  206. omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
  207. data_type, element_count, 1,
  208. OMAP_DMA_SYNC_ELEMENT,
  209. mcspi_dma->dma_tx_sync_dev, 0);
  210. omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
  211. OMAP_DMA_AMODE_CONSTANT,
  212. tx_reg, 0, 0);
  213. omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
  214. OMAP_DMA_AMODE_POST_INC,
  215. xfer->tx_dma, 0, 0);
  216. }
  217. if (rx != NULL) {
  218. omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
  219. data_type, element_count, 1,
  220. OMAP_DMA_SYNC_ELEMENT,
  221. mcspi_dma->dma_rx_sync_dev, 1);
  222. omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
  223. OMAP_DMA_AMODE_CONSTANT,
  224. rx_reg, 0, 0);
  225. omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
  226. OMAP_DMA_AMODE_POST_INC,
  227. xfer->rx_dma, 0, 0);
  228. }
  229. if (tx != NULL) {
  230. omap_start_dma(mcspi_dma->dma_tx_channel);
  231. omap2_mcspi_set_dma_req(spi, 0, 1);
  232. }
  233. if (rx != NULL) {
  234. omap_start_dma(mcspi_dma->dma_rx_channel);
  235. omap2_mcspi_set_dma_req(spi, 1, 1);
  236. }
  237. if (tx != NULL) {
  238. wait_for_completion(&mcspi_dma->dma_tx_completion);
  239. dma_unmap_single(NULL, xfer->tx_dma, count, DMA_TO_DEVICE);
  240. }
  241. if (rx != NULL) {
  242. wait_for_completion(&mcspi_dma->dma_rx_completion);
  243. dma_unmap_single(NULL, xfer->rx_dma, count, DMA_FROM_DEVICE);
  244. }
  245. return count;
  246. }
  247. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  248. {
  249. unsigned long timeout;
  250. timeout = jiffies + msecs_to_jiffies(1000);
  251. while (!(__raw_readl(reg) & bit)) {
  252. if (time_after(jiffies, timeout))
  253. return -1;
  254. cpu_relax();
  255. }
  256. return 0;
  257. }
  258. static unsigned
  259. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  260. {
  261. struct omap2_mcspi *mcspi;
  262. struct omap2_mcspi_cs *cs = spi->controller_state;
  263. unsigned int count, c;
  264. u32 l;
  265. void __iomem *base = cs->base;
  266. void __iomem *tx_reg;
  267. void __iomem *rx_reg;
  268. void __iomem *chstat_reg;
  269. int word_len;
  270. mcspi = spi_master_get_devdata(spi->master);
  271. count = xfer->len;
  272. c = count;
  273. word_len = cs->word_len;
  274. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  275. l &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  276. /* We store the pre-calculated register addresses on stack to speed
  277. * up the transfer loop. */
  278. tx_reg = base + OMAP2_MCSPI_TX0;
  279. rx_reg = base + OMAP2_MCSPI_RX0;
  280. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  281. if (word_len <= 8) {
  282. u8 *rx;
  283. const u8 *tx;
  284. rx = xfer->rx_buf;
  285. tx = xfer->tx_buf;
  286. do {
  287. if (tx != NULL) {
  288. if (mcspi_wait_for_reg_bit(chstat_reg,
  289. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  290. dev_err(&spi->dev, "TXS timed out\n");
  291. goto out;
  292. }
  293. #ifdef VERBOSE
  294. dev_dbg(&spi->dev, "write-%d %02x\n",
  295. word_len, *tx);
  296. #endif
  297. __raw_writel(*tx++, tx_reg);
  298. }
  299. if (rx != NULL) {
  300. if (mcspi_wait_for_reg_bit(chstat_reg,
  301. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  302. dev_err(&spi->dev, "RXS timed out\n");
  303. goto out;
  304. }
  305. /* prevent last RX_ONLY read from triggering
  306. * more word i/o: switch to rx+tx
  307. */
  308. if (c == 0 && tx == NULL)
  309. mcspi_write_cs_reg(spi,
  310. OMAP2_MCSPI_CHCONF0, l);
  311. *rx++ = __raw_readl(rx_reg);
  312. #ifdef VERBOSE
  313. dev_dbg(&spi->dev, "read-%d %02x\n",
  314. word_len, *(rx - 1));
  315. #endif
  316. }
  317. c -= 1;
  318. } while (c);
  319. } else if (word_len <= 16) {
  320. u16 *rx;
  321. const u16 *tx;
  322. rx = xfer->rx_buf;
  323. tx = xfer->tx_buf;
  324. do {
  325. if (tx != NULL) {
  326. if (mcspi_wait_for_reg_bit(chstat_reg,
  327. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  328. dev_err(&spi->dev, "TXS timed out\n");
  329. goto out;
  330. }
  331. #ifdef VERBOSE
  332. dev_dbg(&spi->dev, "write-%d %04x\n",
  333. word_len, *tx);
  334. #endif
  335. __raw_writel(*tx++, tx_reg);
  336. }
  337. if (rx != NULL) {
  338. if (mcspi_wait_for_reg_bit(chstat_reg,
  339. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  340. dev_err(&spi->dev, "RXS timed out\n");
  341. goto out;
  342. }
  343. /* prevent last RX_ONLY read from triggering
  344. * more word i/o: switch to rx+tx
  345. */
  346. if (c == 0 && tx == NULL)
  347. mcspi_write_cs_reg(spi,
  348. OMAP2_MCSPI_CHCONF0, l);
  349. *rx++ = __raw_readl(rx_reg);
  350. #ifdef VERBOSE
  351. dev_dbg(&spi->dev, "read-%d %04x\n",
  352. word_len, *(rx - 1));
  353. #endif
  354. }
  355. c -= 2;
  356. } while (c);
  357. } else if (word_len <= 32) {
  358. u32 *rx;
  359. const u32 *tx;
  360. rx = xfer->rx_buf;
  361. tx = xfer->tx_buf;
  362. do {
  363. if (tx != NULL) {
  364. if (mcspi_wait_for_reg_bit(chstat_reg,
  365. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  366. dev_err(&spi->dev, "TXS timed out\n");
  367. goto out;
  368. }
  369. #ifdef VERBOSE
  370. dev_dbg(&spi->dev, "write-%d %04x\n",
  371. word_len, *tx);
  372. #endif
  373. __raw_writel(*tx++, tx_reg);
  374. }
  375. if (rx != NULL) {
  376. if (mcspi_wait_for_reg_bit(chstat_reg,
  377. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  378. dev_err(&spi->dev, "RXS timed out\n");
  379. goto out;
  380. }
  381. /* prevent last RX_ONLY read from triggering
  382. * more word i/o: switch to rx+tx
  383. */
  384. if (c == 0 && tx == NULL)
  385. mcspi_write_cs_reg(spi,
  386. OMAP2_MCSPI_CHCONF0, l);
  387. *rx++ = __raw_readl(rx_reg);
  388. #ifdef VERBOSE
  389. dev_dbg(&spi->dev, "read-%d %04x\n",
  390. word_len, *(rx - 1));
  391. #endif
  392. }
  393. c -= 4;
  394. } while (c);
  395. }
  396. /* for TX_ONLY mode, be sure all words have shifted out */
  397. if (xfer->rx_buf == NULL) {
  398. if (mcspi_wait_for_reg_bit(chstat_reg,
  399. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  400. dev_err(&spi->dev, "TXS timed out\n");
  401. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  402. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  403. dev_err(&spi->dev, "EOT timed out\n");
  404. }
  405. out:
  406. return count - c;
  407. }
  408. /* called only when no transfer is active to this device */
  409. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  410. struct spi_transfer *t)
  411. {
  412. struct omap2_mcspi_cs *cs = spi->controller_state;
  413. struct omap2_mcspi *mcspi;
  414. u32 l = 0, div = 0;
  415. u8 word_len = spi->bits_per_word;
  416. mcspi = spi_master_get_devdata(spi->master);
  417. if (t != NULL && t->bits_per_word)
  418. word_len = t->bits_per_word;
  419. cs->word_len = word_len;
  420. if (spi->max_speed_hz) {
  421. while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
  422. > spi->max_speed_hz)
  423. div++;
  424. } else
  425. div = 15;
  426. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  427. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  428. * REVISIT: this controller could support SPI_3WIRE mode.
  429. */
  430. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  431. l |= OMAP2_MCSPI_CHCONF_DPE0;
  432. /* wordlength */
  433. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  434. l |= (word_len - 1) << 7;
  435. /* set chipselect polarity; manage with FORCE */
  436. if (!(spi->mode & SPI_CS_HIGH))
  437. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  438. else
  439. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  440. /* set clock divisor */
  441. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  442. l |= div << 2;
  443. /* set SPI mode 0..3 */
  444. if (spi->mode & SPI_CPOL)
  445. l |= OMAP2_MCSPI_CHCONF_POL;
  446. else
  447. l &= ~OMAP2_MCSPI_CHCONF_POL;
  448. if (spi->mode & SPI_CPHA)
  449. l |= OMAP2_MCSPI_CHCONF_PHA;
  450. else
  451. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  452. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  453. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  454. OMAP2_MCSPI_MAX_FREQ / (1 << div),
  455. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  456. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  457. return 0;
  458. }
  459. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  460. {
  461. struct spi_device *spi = data;
  462. struct omap2_mcspi *mcspi;
  463. struct omap2_mcspi_dma *mcspi_dma;
  464. mcspi = spi_master_get_devdata(spi->master);
  465. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  466. complete(&mcspi_dma->dma_rx_completion);
  467. /* We must disable the DMA RX request */
  468. omap2_mcspi_set_dma_req(spi, 1, 0);
  469. }
  470. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  471. {
  472. struct spi_device *spi = data;
  473. struct omap2_mcspi *mcspi;
  474. struct omap2_mcspi_dma *mcspi_dma;
  475. mcspi = spi_master_get_devdata(spi->master);
  476. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  477. complete(&mcspi_dma->dma_tx_completion);
  478. /* We must disable the DMA TX request */
  479. omap2_mcspi_set_dma_req(spi, 0, 0);
  480. }
  481. static int omap2_mcspi_request_dma(struct spi_device *spi)
  482. {
  483. struct spi_master *master = spi->master;
  484. struct omap2_mcspi *mcspi;
  485. struct omap2_mcspi_dma *mcspi_dma;
  486. mcspi = spi_master_get_devdata(master);
  487. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  488. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  489. omap2_mcspi_dma_rx_callback, spi,
  490. &mcspi_dma->dma_rx_channel)) {
  491. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  492. return -EAGAIN;
  493. }
  494. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  495. omap2_mcspi_dma_tx_callback, spi,
  496. &mcspi_dma->dma_tx_channel)) {
  497. omap_free_dma(mcspi_dma->dma_rx_channel);
  498. mcspi_dma->dma_rx_channel = -1;
  499. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  500. return -EAGAIN;
  501. }
  502. init_completion(&mcspi_dma->dma_rx_completion);
  503. init_completion(&mcspi_dma->dma_tx_completion);
  504. return 0;
  505. }
  506. /* the spi->mode bits understood by this driver: */
  507. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  508. static int omap2_mcspi_setup(struct spi_device *spi)
  509. {
  510. int ret;
  511. struct omap2_mcspi *mcspi;
  512. struct omap2_mcspi_dma *mcspi_dma;
  513. struct omap2_mcspi_cs *cs = spi->controller_state;
  514. if (spi->mode & ~MODEBITS) {
  515. dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
  516. spi->mode & ~MODEBITS);
  517. return -EINVAL;
  518. }
  519. if (spi->bits_per_word == 0)
  520. spi->bits_per_word = 8;
  521. else if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  522. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  523. spi->bits_per_word);
  524. return -EINVAL;
  525. }
  526. mcspi = spi_master_get_devdata(spi->master);
  527. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  528. if (!cs) {
  529. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  530. if (!cs)
  531. return -ENOMEM;
  532. cs->base = mcspi->base + spi->chip_select * 0x14;
  533. spi->controller_state = cs;
  534. }
  535. if (mcspi_dma->dma_rx_channel == -1
  536. || mcspi_dma->dma_tx_channel == -1) {
  537. ret = omap2_mcspi_request_dma(spi);
  538. if (ret < 0)
  539. return ret;
  540. }
  541. clk_enable(mcspi->ick);
  542. clk_enable(mcspi->fck);
  543. ret = omap2_mcspi_setup_transfer(spi, NULL);
  544. clk_disable(mcspi->fck);
  545. clk_disable(mcspi->ick);
  546. return ret;
  547. }
  548. static void omap2_mcspi_cleanup(struct spi_device *spi)
  549. {
  550. struct omap2_mcspi *mcspi;
  551. struct omap2_mcspi_dma *mcspi_dma;
  552. mcspi = spi_master_get_devdata(spi->master);
  553. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  554. kfree(spi->controller_state);
  555. if (mcspi_dma->dma_rx_channel != -1) {
  556. omap_free_dma(mcspi_dma->dma_rx_channel);
  557. mcspi_dma->dma_rx_channel = -1;
  558. }
  559. if (mcspi_dma->dma_tx_channel != -1) {
  560. omap_free_dma(mcspi_dma->dma_tx_channel);
  561. mcspi_dma->dma_tx_channel = -1;
  562. }
  563. }
  564. static void omap2_mcspi_work(struct work_struct *work)
  565. {
  566. struct omap2_mcspi *mcspi;
  567. mcspi = container_of(work, struct omap2_mcspi, work);
  568. spin_lock_irq(&mcspi->lock);
  569. clk_enable(mcspi->ick);
  570. clk_enable(mcspi->fck);
  571. /* We only enable one channel at a time -- the one whose message is
  572. * at the head of the queue -- although this controller would gladly
  573. * arbitrate among multiple channels. This corresponds to "single
  574. * channel" master mode. As a side effect, we need to manage the
  575. * chipselect with the FORCE bit ... CS != channel enable.
  576. */
  577. while (!list_empty(&mcspi->msg_queue)) {
  578. struct spi_message *m;
  579. struct spi_device *spi;
  580. struct spi_transfer *t = NULL;
  581. int cs_active = 0;
  582. struct omap2_mcspi_cs *cs;
  583. int par_override = 0;
  584. int status = 0;
  585. u32 chconf;
  586. m = container_of(mcspi->msg_queue.next, struct spi_message,
  587. queue);
  588. list_del_init(&m->queue);
  589. spin_unlock_irq(&mcspi->lock);
  590. spi = m->spi;
  591. cs = spi->controller_state;
  592. omap2_mcspi_set_enable(spi, 1);
  593. list_for_each_entry(t, &m->transfers, transfer_list) {
  594. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  595. status = -EINVAL;
  596. break;
  597. }
  598. if (par_override || t->speed_hz || t->bits_per_word) {
  599. par_override = 1;
  600. status = omap2_mcspi_setup_transfer(spi, t);
  601. if (status < 0)
  602. break;
  603. if (!t->speed_hz && !t->bits_per_word)
  604. par_override = 0;
  605. }
  606. if (!cs_active) {
  607. omap2_mcspi_force_cs(spi, 1);
  608. cs_active = 1;
  609. }
  610. chconf = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  611. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  612. if (t->tx_buf == NULL)
  613. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  614. else if (t->rx_buf == NULL)
  615. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  616. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, chconf);
  617. if (t->len) {
  618. unsigned count;
  619. /* RX_ONLY mode needs dummy data in TX reg */
  620. if (t->tx_buf == NULL)
  621. __raw_writel(0, cs->base
  622. + OMAP2_MCSPI_TX0);
  623. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  624. count = omap2_mcspi_txrx_dma(spi, t);
  625. else
  626. count = omap2_mcspi_txrx_pio(spi, t);
  627. m->actual_length += count;
  628. if (count != t->len) {
  629. status = -EIO;
  630. break;
  631. }
  632. }
  633. if (t->delay_usecs)
  634. udelay(t->delay_usecs);
  635. /* ignore the "leave it on after last xfer" hint */
  636. if (t->cs_change) {
  637. omap2_mcspi_force_cs(spi, 0);
  638. cs_active = 0;
  639. }
  640. }
  641. /* Restore defaults if they were overriden */
  642. if (par_override) {
  643. par_override = 0;
  644. status = omap2_mcspi_setup_transfer(spi, NULL);
  645. }
  646. if (cs_active)
  647. omap2_mcspi_force_cs(spi, 0);
  648. omap2_mcspi_set_enable(spi, 0);
  649. m->status = status;
  650. m->complete(m->context);
  651. spin_lock_irq(&mcspi->lock);
  652. }
  653. clk_disable(mcspi->fck);
  654. clk_disable(mcspi->ick);
  655. spin_unlock_irq(&mcspi->lock);
  656. }
  657. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  658. {
  659. struct omap2_mcspi *mcspi;
  660. unsigned long flags;
  661. struct spi_transfer *t;
  662. m->actual_length = 0;
  663. m->status = 0;
  664. /* reject invalid messages and transfers */
  665. if (list_empty(&m->transfers) || !m->complete)
  666. return -EINVAL;
  667. list_for_each_entry(t, &m->transfers, transfer_list) {
  668. const void *tx_buf = t->tx_buf;
  669. void *rx_buf = t->rx_buf;
  670. unsigned len = t->len;
  671. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  672. || (len && !(rx_buf || tx_buf))
  673. || (t->bits_per_word &&
  674. ( t->bits_per_word < 4
  675. || t->bits_per_word > 32))) {
  676. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  677. t->speed_hz,
  678. len,
  679. tx_buf ? "tx" : "",
  680. rx_buf ? "rx" : "",
  681. t->bits_per_word);
  682. return -EINVAL;
  683. }
  684. if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
  685. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  686. t->speed_hz,
  687. OMAP2_MCSPI_MAX_FREQ/(1<<16));
  688. return -EINVAL;
  689. }
  690. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  691. continue;
  692. /* Do DMA mapping "early" for better error reporting and
  693. * dcache use. Note that if dma_unmap_single() ever starts
  694. * to do real work on ARM, we'd need to clean up mappings
  695. * for previous transfers on *ALL* exits of this loop...
  696. */
  697. if (tx_buf != NULL) {
  698. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  699. len, DMA_TO_DEVICE);
  700. if (dma_mapping_error(t->tx_dma)) {
  701. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  702. 'T', len);
  703. return -EINVAL;
  704. }
  705. }
  706. if (rx_buf != NULL) {
  707. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  708. DMA_FROM_DEVICE);
  709. if (dma_mapping_error(t->rx_dma)) {
  710. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  711. 'R', len);
  712. if (tx_buf != NULL)
  713. dma_unmap_single(NULL, t->tx_dma,
  714. len, DMA_TO_DEVICE);
  715. return -EINVAL;
  716. }
  717. }
  718. }
  719. mcspi = spi_master_get_devdata(spi->master);
  720. spin_lock_irqsave(&mcspi->lock, flags);
  721. list_add_tail(&m->queue, &mcspi->msg_queue);
  722. queue_work(omap2_mcspi_wq, &mcspi->work);
  723. spin_unlock_irqrestore(&mcspi->lock, flags);
  724. return 0;
  725. }
  726. static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
  727. {
  728. struct spi_master *master = mcspi->master;
  729. u32 tmp;
  730. clk_enable(mcspi->ick);
  731. clk_enable(mcspi->fck);
  732. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  733. OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
  734. do {
  735. tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
  736. } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
  737. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  738. /* (3 << 8) | (2 << 3) | */
  739. OMAP2_MCSPI_SYSCONFIG_AUTOIDLE);
  740. omap2_mcspi_set_master_mode(master);
  741. clk_disable(mcspi->fck);
  742. clk_disable(mcspi->ick);
  743. return 0;
  744. }
  745. static u8 __initdata spi1_rxdma_id [] = {
  746. OMAP24XX_DMA_SPI1_RX0,
  747. OMAP24XX_DMA_SPI1_RX1,
  748. OMAP24XX_DMA_SPI1_RX2,
  749. OMAP24XX_DMA_SPI1_RX3,
  750. };
  751. static u8 __initdata spi1_txdma_id [] = {
  752. OMAP24XX_DMA_SPI1_TX0,
  753. OMAP24XX_DMA_SPI1_TX1,
  754. OMAP24XX_DMA_SPI1_TX2,
  755. OMAP24XX_DMA_SPI1_TX3,
  756. };
  757. static u8 __initdata spi2_rxdma_id[] = {
  758. OMAP24XX_DMA_SPI2_RX0,
  759. OMAP24XX_DMA_SPI2_RX1,
  760. };
  761. static u8 __initdata spi2_txdma_id[] = {
  762. OMAP24XX_DMA_SPI2_TX0,
  763. OMAP24XX_DMA_SPI2_TX1,
  764. };
  765. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  766. {
  767. struct spi_master *master;
  768. struct omap2_mcspi *mcspi;
  769. struct resource *r;
  770. int status = 0, i;
  771. const u8 *rxdma_id, *txdma_id;
  772. unsigned num_chipselect;
  773. switch (pdev->id) {
  774. case 1:
  775. rxdma_id = spi1_rxdma_id;
  776. txdma_id = spi1_txdma_id;
  777. num_chipselect = 4;
  778. break;
  779. case 2:
  780. rxdma_id = spi2_rxdma_id;
  781. txdma_id = spi2_txdma_id;
  782. num_chipselect = 2;
  783. break;
  784. /* REVISIT omap2430 has a third McSPI ... */
  785. default:
  786. return -EINVAL;
  787. }
  788. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  789. if (master == NULL) {
  790. dev_dbg(&pdev->dev, "master allocation failed\n");
  791. return -ENOMEM;
  792. }
  793. if (pdev->id != -1)
  794. master->bus_num = pdev->id;
  795. master->setup = omap2_mcspi_setup;
  796. master->transfer = omap2_mcspi_transfer;
  797. master->cleanup = omap2_mcspi_cleanup;
  798. master->num_chipselect = num_chipselect;
  799. dev_set_drvdata(&pdev->dev, master);
  800. mcspi = spi_master_get_devdata(master);
  801. mcspi->master = master;
  802. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  803. if (r == NULL) {
  804. status = -ENODEV;
  805. goto err1;
  806. }
  807. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  808. pdev->dev.bus_id)) {
  809. status = -EBUSY;
  810. goto err1;
  811. }
  812. mcspi->base = (void __iomem *) io_p2v(r->start);
  813. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  814. spin_lock_init(&mcspi->lock);
  815. INIT_LIST_HEAD(&mcspi->msg_queue);
  816. mcspi->ick = clk_get(&pdev->dev, "mcspi_ick");
  817. if (IS_ERR(mcspi->ick)) {
  818. dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
  819. status = PTR_ERR(mcspi->ick);
  820. goto err1a;
  821. }
  822. mcspi->fck = clk_get(&pdev->dev, "mcspi_fck");
  823. if (IS_ERR(mcspi->fck)) {
  824. dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
  825. status = PTR_ERR(mcspi->fck);
  826. goto err2;
  827. }
  828. mcspi->dma_channels = kcalloc(master->num_chipselect,
  829. sizeof(struct omap2_mcspi_dma),
  830. GFP_KERNEL);
  831. if (mcspi->dma_channels == NULL)
  832. goto err3;
  833. for (i = 0; i < num_chipselect; i++) {
  834. mcspi->dma_channels[i].dma_rx_channel = -1;
  835. mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
  836. mcspi->dma_channels[i].dma_tx_channel = -1;
  837. mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
  838. }
  839. if (omap2_mcspi_reset(mcspi) < 0)
  840. goto err4;
  841. status = spi_register_master(master);
  842. if (status < 0)
  843. goto err4;
  844. return status;
  845. err4:
  846. kfree(mcspi->dma_channels);
  847. err3:
  848. clk_put(mcspi->fck);
  849. err2:
  850. clk_put(mcspi->ick);
  851. err1a:
  852. release_mem_region(r->start, (r->end - r->start) + 1);
  853. err1:
  854. spi_master_put(master);
  855. return status;
  856. }
  857. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  858. {
  859. struct spi_master *master;
  860. struct omap2_mcspi *mcspi;
  861. struct omap2_mcspi_dma *dma_channels;
  862. struct resource *r;
  863. master = dev_get_drvdata(&pdev->dev);
  864. mcspi = spi_master_get_devdata(master);
  865. dma_channels = mcspi->dma_channels;
  866. clk_put(mcspi->fck);
  867. clk_put(mcspi->ick);
  868. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  869. release_mem_region(r->start, (r->end - r->start) + 1);
  870. spi_unregister_master(master);
  871. kfree(dma_channels);
  872. return 0;
  873. }
  874. static struct platform_driver omap2_mcspi_driver = {
  875. .driver = {
  876. .name = "omap2_mcspi",
  877. .owner = THIS_MODULE,
  878. },
  879. .remove = __exit_p(omap2_mcspi_remove),
  880. };
  881. static int __init omap2_mcspi_init(void)
  882. {
  883. omap2_mcspi_wq = create_singlethread_workqueue(
  884. omap2_mcspi_driver.driver.name);
  885. if (omap2_mcspi_wq == NULL)
  886. return -1;
  887. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  888. }
  889. subsys_initcall(omap2_mcspi_init);
  890. static void __exit omap2_mcspi_exit(void)
  891. {
  892. platform_driver_unregister(&omap2_mcspi_driver);
  893. destroy_workqueue(omap2_mcspi_wq);
  894. }
  895. module_exit(omap2_mcspi_exit);
  896. MODULE_LICENSE("GPL");