spi_mpc8xxx.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. /*
  2. * MPC8xxx SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. *
  8. * CPM SPI and QE buffer descriptors mode support:
  9. * Copyright (c) 2009 MontaVista Software, Inc.
  10. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bug.h>
  22. #include <linux/errno.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/completion.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/delay.h>
  28. #include <linux/irq.h>
  29. #include <linux/device.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/spi/spi_bitbang.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/fsl_devices.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/mm.h>
  36. #include <linux/mutex.h>
  37. #include <linux/of.h>
  38. #include <linux/of_platform.h>
  39. #include <linux/gpio.h>
  40. #include <linux/of_gpio.h>
  41. #include <linux/of_spi.h>
  42. #include <linux/slab.h>
  43. #include <sysdev/fsl_soc.h>
  44. #include <asm/cpm.h>
  45. #include <asm/qe.h>
  46. #include <asm/irq.h>
  47. /* CPM1 and CPM2 are mutually exclusive. */
  48. #ifdef CONFIG_CPM1
  49. #include <asm/cpm1.h>
  50. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
  51. #else
  52. #include <asm/cpm2.h>
  53. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
  54. #endif
  55. /* SPI Controller registers */
  56. struct mpc8xxx_spi_reg {
  57. u8 res1[0x20];
  58. __be32 mode;
  59. __be32 event;
  60. __be32 mask;
  61. __be32 command;
  62. __be32 transmit;
  63. __be32 receive;
  64. };
  65. /* SPI Controller mode register definitions */
  66. #define SPMODE_LOOP (1 << 30)
  67. #define SPMODE_CI_INACTIVEHIGH (1 << 29)
  68. #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
  69. #define SPMODE_DIV16 (1 << 27)
  70. #define SPMODE_REV (1 << 26)
  71. #define SPMODE_MS (1 << 25)
  72. #define SPMODE_ENABLE (1 << 24)
  73. #define SPMODE_LEN(x) ((x) << 20)
  74. #define SPMODE_PM(x) ((x) << 16)
  75. #define SPMODE_OP (1 << 14)
  76. #define SPMODE_CG(x) ((x) << 7)
  77. /*
  78. * Default for SPI Mode:
  79. * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
  80. */
  81. #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
  82. SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
  83. /* SPIE register values */
  84. #define SPIE_NE 0x00000200 /* Not empty */
  85. #define SPIE_NF 0x00000100 /* Not full */
  86. /* SPIM register values */
  87. #define SPIM_NE 0x00000200 /* Not empty */
  88. #define SPIM_NF 0x00000100 /* Not full */
  89. #define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
  90. #define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
  91. /* SPCOM register values */
  92. #define SPCOM_STR (1 << 23) /* Start transmit */
  93. #define SPI_PRAM_SIZE 0x100
  94. #define SPI_MRBLR ((unsigned int)PAGE_SIZE)
  95. /* SPI Controller driver's private data. */
  96. struct mpc8xxx_spi {
  97. struct device *dev;
  98. struct mpc8xxx_spi_reg __iomem *base;
  99. /* rx & tx bufs from the spi_transfer */
  100. const void *tx;
  101. void *rx;
  102. int subblock;
  103. struct spi_pram __iomem *pram;
  104. struct cpm_buf_desc __iomem *tx_bd;
  105. struct cpm_buf_desc __iomem *rx_bd;
  106. struct spi_transfer *xfer_in_progress;
  107. /* dma addresses for CPM transfers */
  108. dma_addr_t tx_dma;
  109. dma_addr_t rx_dma;
  110. bool map_tx_dma;
  111. bool map_rx_dma;
  112. dma_addr_t dma_dummy_tx;
  113. dma_addr_t dma_dummy_rx;
  114. /* functions to deal with different sized buffers */
  115. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  116. u32(*get_tx) (struct mpc8xxx_spi *);
  117. unsigned int count;
  118. unsigned int irq;
  119. unsigned nsecs; /* (clock cycle time)/2 */
  120. u32 spibrg; /* SPIBRG input clock */
  121. u32 rx_shift; /* RX data reg shift when in qe mode */
  122. u32 tx_shift; /* TX data reg shift when in qe mode */
  123. unsigned int flags;
  124. struct workqueue_struct *workqueue;
  125. struct work_struct work;
  126. struct list_head queue;
  127. spinlock_t lock;
  128. struct completion done;
  129. };
  130. static void *mpc8xxx_dummy_rx;
  131. static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock);
  132. static int mpc8xxx_dummy_rx_refcnt;
  133. struct spi_mpc8xxx_cs {
  134. /* functions to deal with different sized buffers */
  135. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  136. u32 (*get_tx) (struct mpc8xxx_spi *);
  137. u32 rx_shift; /* RX data reg shift when in qe mode */
  138. u32 tx_shift; /* TX data reg shift when in qe mode */
  139. u32 hw_mode; /* Holds HW mode register settings */
  140. };
  141. static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
  142. {
  143. out_be32(reg, val);
  144. }
  145. static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
  146. {
  147. return in_be32(reg);
  148. }
  149. #define MPC83XX_SPI_RX_BUF(type) \
  150. static \
  151. void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
  152. { \
  153. type *rx = mpc8xxx_spi->rx; \
  154. *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
  155. mpc8xxx_spi->rx = rx; \
  156. }
  157. #define MPC83XX_SPI_TX_BUF(type) \
  158. static \
  159. u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
  160. { \
  161. u32 data; \
  162. const type *tx = mpc8xxx_spi->tx; \
  163. if (!tx) \
  164. return 0; \
  165. data = *tx++ << mpc8xxx_spi->tx_shift; \
  166. mpc8xxx_spi->tx = tx; \
  167. return data; \
  168. }
  169. MPC83XX_SPI_RX_BUF(u8)
  170. MPC83XX_SPI_RX_BUF(u16)
  171. MPC83XX_SPI_RX_BUF(u32)
  172. MPC83XX_SPI_TX_BUF(u8)
  173. MPC83XX_SPI_TX_BUF(u16)
  174. MPC83XX_SPI_TX_BUF(u32)
  175. static void mpc8xxx_spi_change_mode(struct spi_device *spi)
  176. {
  177. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  178. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  179. __be32 __iomem *mode = &mspi->base->mode;
  180. unsigned long flags;
  181. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  182. return;
  183. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  184. local_irq_save(flags);
  185. /* Turn off SPI unit prior changing mode */
  186. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  187. /* When in CPM mode, we need to reinit tx and rx. */
  188. if (mspi->flags & SPI_CPM_MODE) {
  189. if (mspi->flags & SPI_QE) {
  190. qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
  191. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  192. } else {
  193. cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
  194. if (mspi->flags & SPI_CPM1) {
  195. out_be16(&mspi->pram->rbptr,
  196. in_be16(&mspi->pram->rbase));
  197. out_be16(&mspi->pram->tbptr,
  198. in_be16(&mspi->pram->tbase));
  199. }
  200. }
  201. }
  202. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  203. local_irq_restore(flags);
  204. }
  205. static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
  206. {
  207. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  208. struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
  209. bool pol = spi->mode & SPI_CS_HIGH;
  210. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  211. if (value == BITBANG_CS_INACTIVE) {
  212. if (pdata->cs_control)
  213. pdata->cs_control(spi, !pol);
  214. }
  215. if (value == BITBANG_CS_ACTIVE) {
  216. mpc8xxx_spi->rx_shift = cs->rx_shift;
  217. mpc8xxx_spi->tx_shift = cs->tx_shift;
  218. mpc8xxx_spi->get_rx = cs->get_rx;
  219. mpc8xxx_spi->get_tx = cs->get_tx;
  220. mpc8xxx_spi_change_mode(spi);
  221. if (pdata->cs_control)
  222. pdata->cs_control(spi, pol);
  223. }
  224. }
  225. static int
  226. mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
  227. struct spi_device *spi,
  228. struct mpc8xxx_spi *mpc8xxx_spi,
  229. int bits_per_word)
  230. {
  231. cs->rx_shift = 0;
  232. cs->tx_shift = 0;
  233. if (bits_per_word <= 8) {
  234. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  235. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  236. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  237. cs->rx_shift = 16;
  238. cs->tx_shift = 24;
  239. }
  240. } else if (bits_per_word <= 16) {
  241. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  242. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  243. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  244. cs->rx_shift = 16;
  245. cs->tx_shift = 16;
  246. }
  247. } else if (bits_per_word <= 32) {
  248. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  249. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  250. } else
  251. return -EINVAL;
  252. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
  253. spi->mode & SPI_LSB_FIRST) {
  254. cs->tx_shift = 0;
  255. if (bits_per_word <= 8)
  256. cs->rx_shift = 8;
  257. else
  258. cs->rx_shift = 0;
  259. }
  260. mpc8xxx_spi->rx_shift = cs->rx_shift;
  261. mpc8xxx_spi->tx_shift = cs->tx_shift;
  262. mpc8xxx_spi->get_rx = cs->get_rx;
  263. mpc8xxx_spi->get_tx = cs->get_tx;
  264. return bits_per_word;
  265. }
  266. static int
  267. mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
  268. struct spi_device *spi,
  269. int bits_per_word)
  270. {
  271. /* QE uses Little Endian for words > 8
  272. * so transform all words > 8 into 8 bits
  273. * Unfortnatly that doesn't work for LSB so
  274. * reject these for now */
  275. /* Note: 32 bits word, LSB works iff
  276. * tfcr/rfcr is set to CPMFCR_GBL */
  277. if (spi->mode & SPI_LSB_FIRST &&
  278. bits_per_word > 8)
  279. return -EINVAL;
  280. if (bits_per_word > 8)
  281. return 8; /* pretend its 8 bits */
  282. return bits_per_word;
  283. }
  284. static
  285. int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  286. {
  287. struct mpc8xxx_spi *mpc8xxx_spi;
  288. int bits_per_word;
  289. u8 pm;
  290. u32 hz;
  291. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  292. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  293. if (t) {
  294. bits_per_word = t->bits_per_word;
  295. hz = t->speed_hz;
  296. } else {
  297. bits_per_word = 0;
  298. hz = 0;
  299. }
  300. /* spi_transfer level calls that work per-word */
  301. if (!bits_per_word)
  302. bits_per_word = spi->bits_per_word;
  303. /* Make sure its a bit width we support [4..16, 32] */
  304. if ((bits_per_word < 4)
  305. || ((bits_per_word > 16) && (bits_per_word != 32)))
  306. return -EINVAL;
  307. if (!hz)
  308. hz = spi->max_speed_hz;
  309. if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
  310. bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
  311. mpc8xxx_spi,
  312. bits_per_word);
  313. else if (mpc8xxx_spi->flags & SPI_QE)
  314. bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
  315. bits_per_word);
  316. if (bits_per_word < 0)
  317. return bits_per_word;
  318. if (bits_per_word == 32)
  319. bits_per_word = 0;
  320. else
  321. bits_per_word = bits_per_word - 1;
  322. /* mask out bits we are going to set */
  323. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  324. | SPMODE_PM(0xF));
  325. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  326. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  327. cs->hw_mode |= SPMODE_DIV16;
  328. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
  329. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  330. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  331. hz, mpc8xxx_spi->spibrg / 1024);
  332. if (pm > 16)
  333. pm = 16;
  334. } else
  335. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
  336. if (pm)
  337. pm--;
  338. cs->hw_mode |= SPMODE_PM(pm);
  339. mpc8xxx_spi_change_mode(spi);
  340. return 0;
  341. }
  342. static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
  343. {
  344. struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
  345. struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
  346. unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
  347. unsigned int xfer_ofs;
  348. xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
  349. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
  350. out_be16(&rx_bd->cbd_datlen, 0);
  351. out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
  352. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
  353. out_be16(&tx_bd->cbd_datlen, xfer_len);
  354. out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
  355. BD_SC_LAST);
  356. /* start transfer */
  357. mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR);
  358. }
  359. static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
  360. struct spi_transfer *t, bool is_dma_mapped)
  361. {
  362. struct device *dev = mspi->dev;
  363. if (is_dma_mapped) {
  364. mspi->map_tx_dma = 0;
  365. mspi->map_rx_dma = 0;
  366. } else {
  367. mspi->map_tx_dma = 1;
  368. mspi->map_rx_dma = 1;
  369. }
  370. if (!t->tx_buf) {
  371. mspi->tx_dma = mspi->dma_dummy_tx;
  372. mspi->map_tx_dma = 0;
  373. }
  374. if (!t->rx_buf) {
  375. mspi->rx_dma = mspi->dma_dummy_rx;
  376. mspi->map_rx_dma = 0;
  377. }
  378. if (mspi->map_tx_dma) {
  379. void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
  380. mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
  381. DMA_TO_DEVICE);
  382. if (dma_mapping_error(dev, mspi->tx_dma)) {
  383. dev_err(dev, "unable to map tx dma\n");
  384. return -ENOMEM;
  385. }
  386. } else if (t->tx_buf) {
  387. mspi->tx_dma = t->tx_dma;
  388. }
  389. if (mspi->map_rx_dma) {
  390. mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
  391. DMA_FROM_DEVICE);
  392. if (dma_mapping_error(dev, mspi->rx_dma)) {
  393. dev_err(dev, "unable to map rx dma\n");
  394. goto err_rx_dma;
  395. }
  396. } else if (t->rx_buf) {
  397. mspi->rx_dma = t->rx_dma;
  398. }
  399. /* enable rx ints */
  400. mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB);
  401. mspi->xfer_in_progress = t;
  402. mspi->count = t->len;
  403. /* start CPM transfers */
  404. mpc8xxx_spi_cpm_bufs_start(mspi);
  405. return 0;
  406. err_rx_dma:
  407. if (mspi->map_tx_dma)
  408. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  409. return -ENOMEM;
  410. }
  411. static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
  412. {
  413. struct device *dev = mspi->dev;
  414. struct spi_transfer *t = mspi->xfer_in_progress;
  415. if (mspi->map_tx_dma)
  416. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  417. if (mspi->map_rx_dma)
  418. dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
  419. mspi->xfer_in_progress = NULL;
  420. }
  421. static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  422. struct spi_transfer *t, unsigned int len)
  423. {
  424. u32 word;
  425. mspi->count = len;
  426. /* enable rx ints */
  427. mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE);
  428. /* transmit word */
  429. word = mspi->get_tx(mspi);
  430. mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
  431. return 0;
  432. }
  433. static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  434. bool is_dma_mapped)
  435. {
  436. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  437. unsigned int len = t->len;
  438. u8 bits_per_word;
  439. int ret;
  440. bits_per_word = spi->bits_per_word;
  441. if (t->bits_per_word)
  442. bits_per_word = t->bits_per_word;
  443. if (bits_per_word > 8) {
  444. /* invalid length? */
  445. if (len & 1)
  446. return -EINVAL;
  447. len /= 2;
  448. }
  449. if (bits_per_word > 16) {
  450. /* invalid length? */
  451. if (len & 1)
  452. return -EINVAL;
  453. len /= 2;
  454. }
  455. mpc8xxx_spi->tx = t->tx_buf;
  456. mpc8xxx_spi->rx = t->rx_buf;
  457. INIT_COMPLETION(mpc8xxx_spi->done);
  458. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  459. ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  460. else
  461. ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len);
  462. if (ret)
  463. return ret;
  464. wait_for_completion(&mpc8xxx_spi->done);
  465. /* disable rx ints */
  466. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  467. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  468. mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi);
  469. return mpc8xxx_spi->count;
  470. }
  471. static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
  472. {
  473. struct spi_device *spi = m->spi;
  474. struct spi_transfer *t;
  475. unsigned int cs_change;
  476. const int nsecs = 50;
  477. int status;
  478. cs_change = 1;
  479. status = 0;
  480. list_for_each_entry(t, &m->transfers, transfer_list) {
  481. if (t->bits_per_word || t->speed_hz) {
  482. /* Don't allow changes if CS is active */
  483. status = -EINVAL;
  484. if (cs_change)
  485. status = mpc8xxx_spi_setup_transfer(spi, t);
  486. if (status < 0)
  487. break;
  488. }
  489. if (cs_change) {
  490. mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  491. ndelay(nsecs);
  492. }
  493. cs_change = t->cs_change;
  494. if (t->len)
  495. status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped);
  496. if (status) {
  497. status = -EMSGSIZE;
  498. break;
  499. }
  500. m->actual_length += t->len;
  501. if (t->delay_usecs)
  502. udelay(t->delay_usecs);
  503. if (cs_change) {
  504. ndelay(nsecs);
  505. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  506. ndelay(nsecs);
  507. }
  508. }
  509. m->status = status;
  510. m->complete(m->context);
  511. if (status || !cs_change) {
  512. ndelay(nsecs);
  513. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  514. }
  515. mpc8xxx_spi_setup_transfer(spi, NULL);
  516. }
  517. static void mpc8xxx_spi_work(struct work_struct *work)
  518. {
  519. struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
  520. work);
  521. spin_lock_irq(&mpc8xxx_spi->lock);
  522. while (!list_empty(&mpc8xxx_spi->queue)) {
  523. struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
  524. struct spi_message, queue);
  525. list_del_init(&m->queue);
  526. spin_unlock_irq(&mpc8xxx_spi->lock);
  527. mpc8xxx_spi_do_one_msg(m);
  528. spin_lock_irq(&mpc8xxx_spi->lock);
  529. }
  530. spin_unlock_irq(&mpc8xxx_spi->lock);
  531. }
  532. static int mpc8xxx_spi_setup(struct spi_device *spi)
  533. {
  534. struct mpc8xxx_spi *mpc8xxx_spi;
  535. int retval;
  536. u32 hw_mode;
  537. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  538. if (!spi->max_speed_hz)
  539. return -EINVAL;
  540. if (!cs) {
  541. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  542. if (!cs)
  543. return -ENOMEM;
  544. spi->controller_state = cs;
  545. }
  546. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  547. hw_mode = cs->hw_mode; /* Save original settings */
  548. cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
  549. /* mask out bits we are going to set */
  550. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  551. | SPMODE_REV | SPMODE_LOOP);
  552. if (spi->mode & SPI_CPHA)
  553. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  554. if (spi->mode & SPI_CPOL)
  555. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  556. if (!(spi->mode & SPI_LSB_FIRST))
  557. cs->hw_mode |= SPMODE_REV;
  558. if (spi->mode & SPI_LOOP)
  559. cs->hw_mode |= SPMODE_LOOP;
  560. retval = mpc8xxx_spi_setup_transfer(spi, NULL);
  561. if (retval < 0) {
  562. cs->hw_mode = hw_mode; /* Restore settings */
  563. return retval;
  564. }
  565. return 0;
  566. }
  567. static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
  568. {
  569. u16 len;
  570. dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
  571. in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
  572. len = in_be16(&mspi->rx_bd->cbd_datlen);
  573. if (len > mspi->count) {
  574. WARN_ON(1);
  575. len = mspi->count;
  576. }
  577. /* Clear the events */
  578. mpc8xxx_spi_write_reg(&mspi->base->event, events);
  579. mspi->count -= len;
  580. if (mspi->count)
  581. mpc8xxx_spi_cpm_bufs_start(mspi);
  582. else
  583. complete(&mspi->done);
  584. }
  585. static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  586. {
  587. /* We need handle RX first */
  588. if (events & SPIE_NE) {
  589. u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive);
  590. if (mspi->rx)
  591. mspi->get_rx(rx_data, mspi);
  592. }
  593. if ((events & SPIE_NF) == 0)
  594. /* spin until TX is done */
  595. while (((events =
  596. mpc8xxx_spi_read_reg(&mspi->base->event)) &
  597. SPIE_NF) == 0)
  598. cpu_relax();
  599. /* Clear the events */
  600. mpc8xxx_spi_write_reg(&mspi->base->event, events);
  601. mspi->count -= 1;
  602. if (mspi->count) {
  603. u32 word = mspi->get_tx(mspi);
  604. mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
  605. } else {
  606. complete(&mspi->done);
  607. }
  608. }
  609. static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
  610. {
  611. struct mpc8xxx_spi *mspi = context_data;
  612. irqreturn_t ret = IRQ_NONE;
  613. u32 events;
  614. /* Get interrupt events(tx/rx) */
  615. events = mpc8xxx_spi_read_reg(&mspi->base->event);
  616. if (events)
  617. ret = IRQ_HANDLED;
  618. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  619. if (mspi->flags & SPI_CPM_MODE)
  620. mpc8xxx_spi_cpm_irq(mspi, events);
  621. else
  622. mpc8xxx_spi_cpu_irq(mspi, events);
  623. return ret;
  624. }
  625. static int mpc8xxx_spi_transfer(struct spi_device *spi,
  626. struct spi_message *m)
  627. {
  628. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  629. unsigned long flags;
  630. m->actual_length = 0;
  631. m->status = -EINPROGRESS;
  632. spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
  633. list_add_tail(&m->queue, &mpc8xxx_spi->queue);
  634. queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
  635. spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
  636. return 0;
  637. }
  638. static void mpc8xxx_spi_cleanup(struct spi_device *spi)
  639. {
  640. kfree(spi->controller_state);
  641. }
  642. static void *mpc8xxx_spi_alloc_dummy_rx(void)
  643. {
  644. mutex_lock(&mpc8xxx_dummy_rx_lock);
  645. if (!mpc8xxx_dummy_rx)
  646. mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
  647. if (mpc8xxx_dummy_rx)
  648. mpc8xxx_dummy_rx_refcnt++;
  649. mutex_unlock(&mpc8xxx_dummy_rx_lock);
  650. return mpc8xxx_dummy_rx;
  651. }
  652. static void mpc8xxx_spi_free_dummy_rx(void)
  653. {
  654. mutex_lock(&mpc8xxx_dummy_rx_lock);
  655. switch (mpc8xxx_dummy_rx_refcnt) {
  656. case 0:
  657. WARN_ON(1);
  658. break;
  659. case 1:
  660. kfree(mpc8xxx_dummy_rx);
  661. mpc8xxx_dummy_rx = NULL;
  662. /* fall through */
  663. default:
  664. mpc8xxx_dummy_rx_refcnt--;
  665. break;
  666. }
  667. mutex_unlock(&mpc8xxx_dummy_rx_lock);
  668. }
  669. static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
  670. {
  671. struct device *dev = mspi->dev;
  672. struct device_node *np = dev->of_node;
  673. const u32 *iprop;
  674. int size;
  675. unsigned long spi_base_ofs;
  676. unsigned long pram_ofs = -ENOMEM;
  677. /* Can't use of_address_to_resource(), QE muram isn't at 0. */
  678. iprop = of_get_property(np, "reg", &size);
  679. /* QE with a fixed pram location? */
  680. if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
  681. return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
  682. /* QE but with a dynamic pram location? */
  683. if (mspi->flags & SPI_QE) {
  684. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  685. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
  686. QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
  687. return pram_ofs;
  688. }
  689. /* CPM1 and CPM2 pram must be at a fixed addr. */
  690. if (!iprop || size != sizeof(*iprop) * 4)
  691. return -ENOMEM;
  692. spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
  693. if (IS_ERR_VALUE(spi_base_ofs))
  694. return -ENOMEM;
  695. if (mspi->flags & SPI_CPM2) {
  696. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  697. if (!IS_ERR_VALUE(pram_ofs)) {
  698. u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs);
  699. out_be16(spi_base, pram_ofs);
  700. }
  701. } else {
  702. struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
  703. u16 rpbase = in_be16(&pram->rpbase);
  704. /* Microcode relocation patch applied? */
  705. if (rpbase)
  706. pram_ofs = rpbase;
  707. else
  708. return spi_base_ofs;
  709. }
  710. cpm_muram_free(spi_base_ofs);
  711. return pram_ofs;
  712. }
  713. static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi)
  714. {
  715. struct device *dev = mspi->dev;
  716. struct device_node *np = dev->of_node;
  717. const u32 *iprop;
  718. int size;
  719. unsigned long pram_ofs;
  720. unsigned long bds_ofs;
  721. if (!(mspi->flags & SPI_CPM_MODE))
  722. return 0;
  723. if (!mpc8xxx_spi_alloc_dummy_rx())
  724. return -ENOMEM;
  725. if (mspi->flags & SPI_QE) {
  726. iprop = of_get_property(np, "cell-index", &size);
  727. if (iprop && size == sizeof(*iprop))
  728. mspi->subblock = *iprop;
  729. switch (mspi->subblock) {
  730. default:
  731. dev_warn(dev, "cell-index unspecified, assuming SPI1");
  732. /* fall through */
  733. case 0:
  734. mspi->subblock = QE_CR_SUBBLOCK_SPI1;
  735. break;
  736. case 1:
  737. mspi->subblock = QE_CR_SUBBLOCK_SPI2;
  738. break;
  739. }
  740. }
  741. pram_ofs = mpc8xxx_spi_cpm_get_pram(mspi);
  742. if (IS_ERR_VALUE(pram_ofs)) {
  743. dev_err(dev, "can't allocate spi parameter ram\n");
  744. goto err_pram;
  745. }
  746. bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
  747. sizeof(*mspi->rx_bd), 8);
  748. if (IS_ERR_VALUE(bds_ofs)) {
  749. dev_err(dev, "can't allocate bds\n");
  750. goto err_bds;
  751. }
  752. mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
  753. DMA_TO_DEVICE);
  754. if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
  755. dev_err(dev, "unable to map dummy tx buffer\n");
  756. goto err_dummy_tx;
  757. }
  758. mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR,
  759. DMA_FROM_DEVICE);
  760. if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
  761. dev_err(dev, "unable to map dummy rx buffer\n");
  762. goto err_dummy_rx;
  763. }
  764. mspi->pram = cpm_muram_addr(pram_ofs);
  765. mspi->tx_bd = cpm_muram_addr(bds_ofs);
  766. mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
  767. /* Initialize parameter ram. */
  768. out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
  769. out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
  770. out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
  771. out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
  772. out_be16(&mspi->pram->mrblr, SPI_MRBLR);
  773. out_be32(&mspi->pram->rstate, 0);
  774. out_be32(&mspi->pram->rdp, 0);
  775. out_be16(&mspi->pram->rbptr, 0);
  776. out_be16(&mspi->pram->rbc, 0);
  777. out_be32(&mspi->pram->rxtmp, 0);
  778. out_be32(&mspi->pram->tstate, 0);
  779. out_be32(&mspi->pram->tdp, 0);
  780. out_be16(&mspi->pram->tbptr, 0);
  781. out_be16(&mspi->pram->tbc, 0);
  782. out_be32(&mspi->pram->txtmp, 0);
  783. return 0;
  784. err_dummy_rx:
  785. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  786. err_dummy_tx:
  787. cpm_muram_free(bds_ofs);
  788. err_bds:
  789. cpm_muram_free(pram_ofs);
  790. err_pram:
  791. mpc8xxx_spi_free_dummy_rx();
  792. return -ENOMEM;
  793. }
  794. static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi)
  795. {
  796. struct device *dev = mspi->dev;
  797. dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
  798. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  799. cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
  800. cpm_muram_free(cpm_muram_offset(mspi->pram));
  801. mpc8xxx_spi_free_dummy_rx();
  802. }
  803. static const char *mpc8xxx_spi_strmode(unsigned int flags)
  804. {
  805. if (flags & SPI_QE_CPU_MODE) {
  806. return "QE CPU";
  807. } else if (flags & SPI_CPM_MODE) {
  808. if (flags & SPI_QE)
  809. return "QE";
  810. else if (flags & SPI_CPM2)
  811. return "CPM2";
  812. else
  813. return "CPM1";
  814. }
  815. return "CPU";
  816. }
  817. static struct spi_master * __devinit
  818. mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
  819. {
  820. struct fsl_spi_platform_data *pdata = dev->platform_data;
  821. struct spi_master *master;
  822. struct mpc8xxx_spi *mpc8xxx_spi;
  823. u32 regval;
  824. int ret = 0;
  825. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  826. if (master == NULL) {
  827. ret = -ENOMEM;
  828. goto err;
  829. }
  830. dev_set_drvdata(dev, master);
  831. /* the spi->mode bits understood by this driver: */
  832. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  833. | SPI_LSB_FIRST | SPI_LOOP;
  834. master->setup = mpc8xxx_spi_setup;
  835. master->transfer = mpc8xxx_spi_transfer;
  836. master->cleanup = mpc8xxx_spi_cleanup;
  837. mpc8xxx_spi = spi_master_get_devdata(master);
  838. mpc8xxx_spi->dev = dev;
  839. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  840. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  841. mpc8xxx_spi->flags = pdata->flags;
  842. mpc8xxx_spi->spibrg = pdata->sysclk;
  843. ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi);
  844. if (ret)
  845. goto err_cpm_init;
  846. mpc8xxx_spi->rx_shift = 0;
  847. mpc8xxx_spi->tx_shift = 0;
  848. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  849. mpc8xxx_spi->rx_shift = 16;
  850. mpc8xxx_spi->tx_shift = 24;
  851. }
  852. init_completion(&mpc8xxx_spi->done);
  853. mpc8xxx_spi->base = ioremap(mem->start, resource_size(mem));
  854. if (mpc8xxx_spi->base == NULL) {
  855. ret = -ENOMEM;
  856. goto err_ioremap;
  857. }
  858. mpc8xxx_spi->irq = irq;
  859. /* Register for SPI Interrupt */
  860. ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
  861. 0, "mpc8xxx_spi", mpc8xxx_spi);
  862. if (ret != 0)
  863. goto unmap_io;
  864. master->bus_num = pdata->bus_num;
  865. master->num_chipselect = pdata->max_chipselect;
  866. /* SPI controller initializations */
  867. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
  868. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  869. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
  870. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
  871. /* Enable SPI interface */
  872. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  873. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  874. regval |= SPMODE_OP;
  875. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
  876. spin_lock_init(&mpc8xxx_spi->lock);
  877. init_completion(&mpc8xxx_spi->done);
  878. INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
  879. INIT_LIST_HEAD(&mpc8xxx_spi->queue);
  880. mpc8xxx_spi->workqueue = create_singlethread_workqueue(
  881. dev_name(master->dev.parent));
  882. if (mpc8xxx_spi->workqueue == NULL) {
  883. ret = -EBUSY;
  884. goto free_irq;
  885. }
  886. ret = spi_register_master(master);
  887. if (ret < 0)
  888. goto unreg_master;
  889. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base,
  890. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  891. return master;
  892. unreg_master:
  893. destroy_workqueue(mpc8xxx_spi->workqueue);
  894. free_irq:
  895. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  896. unmap_io:
  897. iounmap(mpc8xxx_spi->base);
  898. err_ioremap:
  899. mpc8xxx_spi_cpm_free(mpc8xxx_spi);
  900. err_cpm_init:
  901. spi_master_put(master);
  902. err:
  903. return ERR_PTR(ret);
  904. }
  905. static int __devexit mpc8xxx_spi_remove(struct device *dev)
  906. {
  907. struct mpc8xxx_spi *mpc8xxx_spi;
  908. struct spi_master *master;
  909. master = dev_get_drvdata(dev);
  910. mpc8xxx_spi = spi_master_get_devdata(master);
  911. flush_workqueue(mpc8xxx_spi->workqueue);
  912. destroy_workqueue(mpc8xxx_spi->workqueue);
  913. spi_unregister_master(master);
  914. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  915. iounmap(mpc8xxx_spi->base);
  916. mpc8xxx_spi_cpm_free(mpc8xxx_spi);
  917. return 0;
  918. }
  919. struct mpc8xxx_spi_probe_info {
  920. struct fsl_spi_platform_data pdata;
  921. int *gpios;
  922. bool *alow_flags;
  923. };
  924. static struct mpc8xxx_spi_probe_info *
  925. to_of_pinfo(struct fsl_spi_platform_data *pdata)
  926. {
  927. return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
  928. }
  929. static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
  930. {
  931. struct device *dev = spi->dev.parent;
  932. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  933. u16 cs = spi->chip_select;
  934. int gpio = pinfo->gpios[cs];
  935. bool alow = pinfo->alow_flags[cs];
  936. gpio_set_value(gpio, on ^ alow);
  937. }
  938. static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
  939. {
  940. struct device_node *np = dev->of_node;
  941. struct fsl_spi_platform_data *pdata = dev->platform_data;
  942. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  943. unsigned int ngpios;
  944. int i = 0;
  945. int ret;
  946. ngpios = of_gpio_count(np);
  947. if (!ngpios) {
  948. /*
  949. * SPI w/o chip-select line. One SPI device is still permitted
  950. * though.
  951. */
  952. pdata->max_chipselect = 1;
  953. return 0;
  954. }
  955. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  956. if (!pinfo->gpios)
  957. return -ENOMEM;
  958. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  959. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  960. GFP_KERNEL);
  961. if (!pinfo->alow_flags) {
  962. ret = -ENOMEM;
  963. goto err_alloc_flags;
  964. }
  965. for (; i < ngpios; i++) {
  966. int gpio;
  967. enum of_gpio_flags flags;
  968. gpio = of_get_gpio_flags(np, i, &flags);
  969. if (!gpio_is_valid(gpio)) {
  970. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  971. ret = gpio;
  972. goto err_loop;
  973. }
  974. ret = gpio_request(gpio, dev_name(dev));
  975. if (ret) {
  976. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  977. goto err_loop;
  978. }
  979. pinfo->gpios[i] = gpio;
  980. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  981. ret = gpio_direction_output(pinfo->gpios[i],
  982. pinfo->alow_flags[i]);
  983. if (ret) {
  984. dev_err(dev, "can't set output direction for gpio "
  985. "#%d: %d\n", i, ret);
  986. goto err_loop;
  987. }
  988. }
  989. pdata->max_chipselect = ngpios;
  990. pdata->cs_control = mpc8xxx_spi_cs_control;
  991. return 0;
  992. err_loop:
  993. while (i >= 0) {
  994. if (gpio_is_valid(pinfo->gpios[i]))
  995. gpio_free(pinfo->gpios[i]);
  996. i--;
  997. }
  998. kfree(pinfo->alow_flags);
  999. pinfo->alow_flags = NULL;
  1000. err_alloc_flags:
  1001. kfree(pinfo->gpios);
  1002. pinfo->gpios = NULL;
  1003. return ret;
  1004. }
  1005. static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
  1006. {
  1007. struct fsl_spi_platform_data *pdata = dev->platform_data;
  1008. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  1009. int i;
  1010. if (!pinfo->gpios)
  1011. return 0;
  1012. for (i = 0; i < pdata->max_chipselect; i++) {
  1013. if (gpio_is_valid(pinfo->gpios[i]))
  1014. gpio_free(pinfo->gpios[i]);
  1015. }
  1016. kfree(pinfo->gpios);
  1017. kfree(pinfo->alow_flags);
  1018. return 0;
  1019. }
  1020. static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
  1021. const struct of_device_id *ofid)
  1022. {
  1023. struct device *dev = &ofdev->dev;
  1024. struct device_node *np = ofdev->dev.of_node;
  1025. struct mpc8xxx_spi_probe_info *pinfo;
  1026. struct fsl_spi_platform_data *pdata;
  1027. struct spi_master *master;
  1028. struct resource mem;
  1029. struct resource irq;
  1030. const void *prop;
  1031. int ret = -ENOMEM;
  1032. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  1033. if (!pinfo)
  1034. return -ENOMEM;
  1035. pdata = &pinfo->pdata;
  1036. dev->platform_data = pdata;
  1037. /* Allocate bus num dynamically. */
  1038. pdata->bus_num = -1;
  1039. /* SPI controller is either clocked from QE or SoC clock. */
  1040. pdata->sysclk = get_brgfreq();
  1041. if (pdata->sysclk == -1) {
  1042. pdata->sysclk = fsl_get_sys_freq();
  1043. if (pdata->sysclk == -1) {
  1044. ret = -ENODEV;
  1045. goto err_clk;
  1046. }
  1047. }
  1048. prop = of_get_property(np, "mode", NULL);
  1049. if (prop && !strcmp(prop, "cpu-qe"))
  1050. pdata->flags = SPI_QE_CPU_MODE;
  1051. else if (prop && !strcmp(prop, "qe"))
  1052. pdata->flags = SPI_CPM_MODE | SPI_QE;
  1053. else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
  1054. pdata->flags = SPI_CPM_MODE | SPI_CPM2;
  1055. else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
  1056. pdata->flags = SPI_CPM_MODE | SPI_CPM1;
  1057. ret = of_mpc8xxx_spi_get_chipselects(dev);
  1058. if (ret)
  1059. goto err;
  1060. ret = of_address_to_resource(np, 0, &mem);
  1061. if (ret)
  1062. goto err;
  1063. ret = of_irq_to_resource(np, 0, &irq);
  1064. if (!ret) {
  1065. ret = -EINVAL;
  1066. goto err;
  1067. }
  1068. master = mpc8xxx_spi_probe(dev, &mem, irq.start);
  1069. if (IS_ERR(master)) {
  1070. ret = PTR_ERR(master);
  1071. goto err;
  1072. }
  1073. of_register_spi_devices(master, np);
  1074. return 0;
  1075. err:
  1076. of_mpc8xxx_spi_free_chipselects(dev);
  1077. err_clk:
  1078. kfree(pinfo);
  1079. return ret;
  1080. }
  1081. static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev)
  1082. {
  1083. int ret;
  1084. ret = mpc8xxx_spi_remove(&ofdev->dev);
  1085. if (ret)
  1086. return ret;
  1087. of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
  1088. return 0;
  1089. }
  1090. static const struct of_device_id of_mpc8xxx_spi_match[] = {
  1091. { .compatible = "fsl,spi" },
  1092. {},
  1093. };
  1094. MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
  1095. static struct of_platform_driver of_mpc8xxx_spi_driver = {
  1096. .driver = {
  1097. .name = "mpc8xxx_spi",
  1098. .owner = THIS_MODULE,
  1099. .of_match_table = of_mpc8xxx_spi_match,
  1100. },
  1101. .probe = of_mpc8xxx_spi_probe,
  1102. .remove = __devexit_p(of_mpc8xxx_spi_remove),
  1103. };
  1104. #ifdef CONFIG_MPC832x_RDB
  1105. /*
  1106. * XXX XXX XXX
  1107. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  1108. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  1109. * tree can work with OpenFirmware driver. But for now we support old trees
  1110. * as well.
  1111. */
  1112. static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  1113. {
  1114. struct resource *mem;
  1115. int irq;
  1116. struct spi_master *master;
  1117. if (!pdev->dev.platform_data)
  1118. return -EINVAL;
  1119. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1120. if (!mem)
  1121. return -EINVAL;
  1122. irq = platform_get_irq(pdev, 0);
  1123. if (irq <= 0)
  1124. return -EINVAL;
  1125. master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
  1126. if (IS_ERR(master))
  1127. return PTR_ERR(master);
  1128. return 0;
  1129. }
  1130. static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  1131. {
  1132. return mpc8xxx_spi_remove(&pdev->dev);
  1133. }
  1134. MODULE_ALIAS("platform:mpc8xxx_spi");
  1135. static struct platform_driver mpc8xxx_spi_driver = {
  1136. .probe = plat_mpc8xxx_spi_probe,
  1137. .remove = __devexit_p(plat_mpc8xxx_spi_remove),
  1138. .driver = {
  1139. .name = "mpc8xxx_spi",
  1140. .owner = THIS_MODULE,
  1141. },
  1142. };
  1143. static bool legacy_driver_failed;
  1144. static void __init legacy_driver_register(void)
  1145. {
  1146. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  1147. }
  1148. static void __exit legacy_driver_unregister(void)
  1149. {
  1150. if (legacy_driver_failed)
  1151. return;
  1152. platform_driver_unregister(&mpc8xxx_spi_driver);
  1153. }
  1154. #else
  1155. static void __init legacy_driver_register(void) {}
  1156. static void __exit legacy_driver_unregister(void) {}
  1157. #endif /* CONFIG_MPC832x_RDB */
  1158. static int __init mpc8xxx_spi_init(void)
  1159. {
  1160. legacy_driver_register();
  1161. return of_register_platform_driver(&of_mpc8xxx_spi_driver);
  1162. }
  1163. static void __exit mpc8xxx_spi_exit(void)
  1164. {
  1165. of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
  1166. legacy_driver_unregister();
  1167. }
  1168. module_init(mpc8xxx_spi_init);
  1169. module_exit(mpc8xxx_spi_exit);
  1170. MODULE_AUTHOR("Kumar Gala");
  1171. MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
  1172. MODULE_LICENSE("GPL");