spi-pxa2xx.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /*
  2. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  3. * Copyright (C) 2013, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <linux/ioport.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/spi/pxa2xx_spi.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/delay.h>
  27. #include <linux/gpio.h>
  28. #include <linux/slab.h>
  29. #include <linux/clk.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/acpi.h>
  32. #include "spi-pxa2xx.h"
  33. MODULE_AUTHOR("Stephen Street");
  34. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  35. MODULE_LICENSE("GPL");
  36. MODULE_ALIAS("platform:pxa2xx-spi");
  37. #define TIMOUT_DFLT 1000
  38. /*
  39. * for testing SSCR1 changes that require SSP restart, basically
  40. * everything except the service and interrupt enables, the pxa270 developer
  41. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  42. * list, but the PXA255 dev man says all bits without really meaning the
  43. * service and interrupt enables
  44. */
  45. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  46. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  47. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  48. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  49. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  50. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  51. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  52. | QUARK_X1000_SSCR1_EFWR \
  53. | QUARK_X1000_SSCR1_RFT \
  54. | QUARK_X1000_SSCR1_TFT \
  55. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  56. #define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  57. #define SPI_CS_CONTROL_SW_MODE BIT(0)
  58. #define SPI_CS_CONTROL_CS_HIGH BIT(1)
  59. struct lpss_config {
  60. /* LPSS offset from drv_data->ioaddr */
  61. unsigned offset;
  62. /* Register offsets from drv_data->lpss_base or -1 */
  63. int reg_general;
  64. int reg_ssp;
  65. int reg_cs_ctrl;
  66. /* FIFO thresholds */
  67. u32 rx_threshold;
  68. u32 tx_threshold_lo;
  69. u32 tx_threshold_hi;
  70. };
  71. /* Keep these sorted with enum pxa_ssp_type */
  72. static const struct lpss_config lpss_platforms[] = {
  73. { /* LPSS_LPT_SSP */
  74. .offset = 0x800,
  75. .reg_general = 0x08,
  76. .reg_ssp = 0x0c,
  77. .reg_cs_ctrl = 0x18,
  78. .rx_threshold = 64,
  79. .tx_threshold_lo = 160,
  80. .tx_threshold_hi = 224,
  81. },
  82. { /* LPSS_BYT_SSP */
  83. .offset = 0x400,
  84. .reg_general = 0x08,
  85. .reg_ssp = 0x0c,
  86. .reg_cs_ctrl = 0x18,
  87. .rx_threshold = 64,
  88. .tx_threshold_lo = 160,
  89. .tx_threshold_hi = 224,
  90. },
  91. };
  92. static inline const struct lpss_config
  93. *lpss_get_config(const struct driver_data *drv_data)
  94. {
  95. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  96. }
  97. static bool is_lpss_ssp(const struct driver_data *drv_data)
  98. {
  99. switch (drv_data->ssp_type) {
  100. case LPSS_LPT_SSP:
  101. case LPSS_BYT_SSP:
  102. return true;
  103. default:
  104. return false;
  105. }
  106. }
  107. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  108. {
  109. return drv_data->ssp_type == QUARK_X1000_SSP;
  110. }
  111. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  112. {
  113. switch (drv_data->ssp_type) {
  114. case QUARK_X1000_SSP:
  115. return QUARK_X1000_SSCR1_CHANGE_MASK;
  116. default:
  117. return SSCR1_CHANGE_MASK;
  118. }
  119. }
  120. static u32
  121. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  122. {
  123. switch (drv_data->ssp_type) {
  124. case QUARK_X1000_SSP:
  125. return RX_THRESH_QUARK_X1000_DFLT;
  126. default:
  127. return RX_THRESH_DFLT;
  128. }
  129. }
  130. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  131. {
  132. u32 mask;
  133. switch (drv_data->ssp_type) {
  134. case QUARK_X1000_SSP:
  135. mask = QUARK_X1000_SSSR_TFL_MASK;
  136. break;
  137. default:
  138. mask = SSSR_TFL_MASK;
  139. break;
  140. }
  141. return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
  142. }
  143. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  144. u32 *sccr1_reg)
  145. {
  146. u32 mask;
  147. switch (drv_data->ssp_type) {
  148. case QUARK_X1000_SSP:
  149. mask = QUARK_X1000_SSCR1_RFT;
  150. break;
  151. default:
  152. mask = SSCR1_RFT;
  153. break;
  154. }
  155. *sccr1_reg &= ~mask;
  156. }
  157. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  158. u32 *sccr1_reg, u32 threshold)
  159. {
  160. switch (drv_data->ssp_type) {
  161. case QUARK_X1000_SSP:
  162. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  163. break;
  164. default:
  165. *sccr1_reg |= SSCR1_RxTresh(threshold);
  166. break;
  167. }
  168. }
  169. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  170. u32 clk_div, u8 bits)
  171. {
  172. switch (drv_data->ssp_type) {
  173. case QUARK_X1000_SSP:
  174. return clk_div
  175. | QUARK_X1000_SSCR0_Motorola
  176. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
  177. | SSCR0_SSE;
  178. default:
  179. return clk_div
  180. | SSCR0_Motorola
  181. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  182. | SSCR0_SSE
  183. | (bits > 16 ? SSCR0_EDSS : 0);
  184. }
  185. }
  186. /*
  187. * Read and write LPSS SSP private registers. Caller must first check that
  188. * is_lpss_ssp() returns true before these can be called.
  189. */
  190. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  191. {
  192. WARN_ON(!drv_data->lpss_base);
  193. return readl(drv_data->lpss_base + offset);
  194. }
  195. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  196. unsigned offset, u32 value)
  197. {
  198. WARN_ON(!drv_data->lpss_base);
  199. writel(value, drv_data->lpss_base + offset);
  200. }
  201. /*
  202. * lpss_ssp_setup - perform LPSS SSP specific setup
  203. * @drv_data: pointer to the driver private data
  204. *
  205. * Perform LPSS SSP specific setup. This function must be called first if
  206. * one is going to use LPSS SSP private registers.
  207. */
  208. static void lpss_ssp_setup(struct driver_data *drv_data)
  209. {
  210. const struct lpss_config *config;
  211. u32 value;
  212. config = lpss_get_config(drv_data);
  213. drv_data->lpss_base = drv_data->ioaddr + config->offset;
  214. /* Enable software chip select control */
  215. value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH;
  216. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  217. /* Enable multiblock DMA transfers */
  218. if (drv_data->master_info->enable_dma) {
  219. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  220. if (config->reg_general >= 0) {
  221. value = __lpss_ssp_read_priv(drv_data,
  222. config->reg_general);
  223. value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  224. __lpss_ssp_write_priv(drv_data,
  225. config->reg_general, value);
  226. }
  227. }
  228. }
  229. static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
  230. {
  231. const struct lpss_config *config;
  232. u32 value;
  233. config = lpss_get_config(drv_data);
  234. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  235. if (enable)
  236. value &= ~SPI_CS_CONTROL_CS_HIGH;
  237. else
  238. value |= SPI_CS_CONTROL_CS_HIGH;
  239. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  240. }
  241. static void cs_assert(struct driver_data *drv_data)
  242. {
  243. struct chip_data *chip = drv_data->cur_chip;
  244. if (drv_data->ssp_type == CE4100_SSP) {
  245. pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
  246. return;
  247. }
  248. if (chip->cs_control) {
  249. chip->cs_control(PXA2XX_CS_ASSERT);
  250. return;
  251. }
  252. if (gpio_is_valid(chip->gpio_cs)) {
  253. gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
  254. return;
  255. }
  256. if (is_lpss_ssp(drv_data))
  257. lpss_ssp_cs_control(drv_data, true);
  258. }
  259. static void cs_deassert(struct driver_data *drv_data)
  260. {
  261. struct chip_data *chip = drv_data->cur_chip;
  262. if (drv_data->ssp_type == CE4100_SSP)
  263. return;
  264. if (chip->cs_control) {
  265. chip->cs_control(PXA2XX_CS_DEASSERT);
  266. return;
  267. }
  268. if (gpio_is_valid(chip->gpio_cs)) {
  269. gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
  270. return;
  271. }
  272. if (is_lpss_ssp(drv_data))
  273. lpss_ssp_cs_control(drv_data, false);
  274. }
  275. int pxa2xx_spi_flush(struct driver_data *drv_data)
  276. {
  277. unsigned long limit = loops_per_jiffy << 1;
  278. do {
  279. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  280. pxa2xx_spi_read(drv_data, SSDR);
  281. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  282. write_SSSR_CS(drv_data, SSSR_ROR);
  283. return limit;
  284. }
  285. static int null_writer(struct driver_data *drv_data)
  286. {
  287. u8 n_bytes = drv_data->n_bytes;
  288. if (pxa2xx_spi_txfifo_full(drv_data)
  289. || (drv_data->tx == drv_data->tx_end))
  290. return 0;
  291. pxa2xx_spi_write(drv_data, SSDR, 0);
  292. drv_data->tx += n_bytes;
  293. return 1;
  294. }
  295. static int null_reader(struct driver_data *drv_data)
  296. {
  297. u8 n_bytes = drv_data->n_bytes;
  298. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  299. && (drv_data->rx < drv_data->rx_end)) {
  300. pxa2xx_spi_read(drv_data, SSDR);
  301. drv_data->rx += n_bytes;
  302. }
  303. return drv_data->rx == drv_data->rx_end;
  304. }
  305. static int u8_writer(struct driver_data *drv_data)
  306. {
  307. if (pxa2xx_spi_txfifo_full(drv_data)
  308. || (drv_data->tx == drv_data->tx_end))
  309. return 0;
  310. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  311. ++drv_data->tx;
  312. return 1;
  313. }
  314. static int u8_reader(struct driver_data *drv_data)
  315. {
  316. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  317. && (drv_data->rx < drv_data->rx_end)) {
  318. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  319. ++drv_data->rx;
  320. }
  321. return drv_data->rx == drv_data->rx_end;
  322. }
  323. static int u16_writer(struct driver_data *drv_data)
  324. {
  325. if (pxa2xx_spi_txfifo_full(drv_data)
  326. || (drv_data->tx == drv_data->tx_end))
  327. return 0;
  328. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  329. drv_data->tx += 2;
  330. return 1;
  331. }
  332. static int u16_reader(struct driver_data *drv_data)
  333. {
  334. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  335. && (drv_data->rx < drv_data->rx_end)) {
  336. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  337. drv_data->rx += 2;
  338. }
  339. return drv_data->rx == drv_data->rx_end;
  340. }
  341. static int u32_writer(struct driver_data *drv_data)
  342. {
  343. if (pxa2xx_spi_txfifo_full(drv_data)
  344. || (drv_data->tx == drv_data->tx_end))
  345. return 0;
  346. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  347. drv_data->tx += 4;
  348. return 1;
  349. }
  350. static int u32_reader(struct driver_data *drv_data)
  351. {
  352. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  353. && (drv_data->rx < drv_data->rx_end)) {
  354. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  355. drv_data->rx += 4;
  356. }
  357. return drv_data->rx == drv_data->rx_end;
  358. }
  359. void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
  360. {
  361. struct spi_message *msg = drv_data->cur_msg;
  362. struct spi_transfer *trans = drv_data->cur_transfer;
  363. /* Move to next transfer */
  364. if (trans->transfer_list.next != &msg->transfers) {
  365. drv_data->cur_transfer =
  366. list_entry(trans->transfer_list.next,
  367. struct spi_transfer,
  368. transfer_list);
  369. return RUNNING_STATE;
  370. } else
  371. return DONE_STATE;
  372. }
  373. /* caller already set message->status; dma and pio irqs are blocked */
  374. static void giveback(struct driver_data *drv_data)
  375. {
  376. struct spi_transfer* last_transfer;
  377. struct spi_message *msg;
  378. msg = drv_data->cur_msg;
  379. drv_data->cur_msg = NULL;
  380. drv_data->cur_transfer = NULL;
  381. last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
  382. transfer_list);
  383. /* Delay if requested before any change in chip select */
  384. if (last_transfer->delay_usecs)
  385. udelay(last_transfer->delay_usecs);
  386. /* Drop chip select UNLESS cs_change is true or we are returning
  387. * a message with an error, or next message is for another chip
  388. */
  389. if (!last_transfer->cs_change)
  390. cs_deassert(drv_data);
  391. else {
  392. struct spi_message *next_msg;
  393. /* Holding of cs was hinted, but we need to make sure
  394. * the next message is for the same chip. Don't waste
  395. * time with the following tests unless this was hinted.
  396. *
  397. * We cannot postpone this until pump_messages, because
  398. * after calling msg->complete (below) the driver that
  399. * sent the current message could be unloaded, which
  400. * could invalidate the cs_control() callback...
  401. */
  402. /* get a pointer to the next message, if any */
  403. next_msg = spi_get_next_queued_message(drv_data->master);
  404. /* see if the next and current messages point
  405. * to the same chip
  406. */
  407. if (next_msg && next_msg->spi != msg->spi)
  408. next_msg = NULL;
  409. if (!next_msg || msg->state == ERROR_STATE)
  410. cs_deassert(drv_data);
  411. }
  412. drv_data->cur_chip = NULL;
  413. spi_finalize_current_message(drv_data->master);
  414. }
  415. static void reset_sccr1(struct driver_data *drv_data)
  416. {
  417. struct chip_data *chip = drv_data->cur_chip;
  418. u32 sccr1_reg;
  419. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  420. sccr1_reg &= ~SSCR1_RFT;
  421. sccr1_reg |= chip->threshold;
  422. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  423. }
  424. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  425. {
  426. /* Stop and reset SSP */
  427. write_SSSR_CS(drv_data, drv_data->clear_sr);
  428. reset_sccr1(drv_data);
  429. if (!pxa25x_ssp_comp(drv_data))
  430. pxa2xx_spi_write(drv_data, SSTO, 0);
  431. pxa2xx_spi_flush(drv_data);
  432. pxa2xx_spi_write(drv_data, SSCR0,
  433. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  434. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  435. drv_data->cur_msg->state = ERROR_STATE;
  436. tasklet_schedule(&drv_data->pump_transfers);
  437. }
  438. static void int_transfer_complete(struct driver_data *drv_data)
  439. {
  440. /* Stop SSP */
  441. write_SSSR_CS(drv_data, drv_data->clear_sr);
  442. reset_sccr1(drv_data);
  443. if (!pxa25x_ssp_comp(drv_data))
  444. pxa2xx_spi_write(drv_data, SSTO, 0);
  445. /* Update total byte transferred return count actual bytes read */
  446. drv_data->cur_msg->actual_length += drv_data->len -
  447. (drv_data->rx_end - drv_data->rx);
  448. /* Transfer delays and chip select release are
  449. * handled in pump_transfers or giveback
  450. */
  451. /* Move to next transfer */
  452. drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
  453. /* Schedule transfer tasklet */
  454. tasklet_schedule(&drv_data->pump_transfers);
  455. }
  456. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  457. {
  458. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  459. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  460. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  461. if (irq_status & SSSR_ROR) {
  462. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  463. return IRQ_HANDLED;
  464. }
  465. if (irq_status & SSSR_TINT) {
  466. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  467. if (drv_data->read(drv_data)) {
  468. int_transfer_complete(drv_data);
  469. return IRQ_HANDLED;
  470. }
  471. }
  472. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  473. do {
  474. if (drv_data->read(drv_data)) {
  475. int_transfer_complete(drv_data);
  476. return IRQ_HANDLED;
  477. }
  478. } while (drv_data->write(drv_data));
  479. if (drv_data->read(drv_data)) {
  480. int_transfer_complete(drv_data);
  481. return IRQ_HANDLED;
  482. }
  483. if (drv_data->tx == drv_data->tx_end) {
  484. u32 bytes_left;
  485. u32 sccr1_reg;
  486. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  487. sccr1_reg &= ~SSCR1_TIE;
  488. /*
  489. * PXA25x_SSP has no timeout, set up rx threshould for the
  490. * remaining RX bytes.
  491. */
  492. if (pxa25x_ssp_comp(drv_data)) {
  493. u32 rx_thre;
  494. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  495. bytes_left = drv_data->rx_end - drv_data->rx;
  496. switch (drv_data->n_bytes) {
  497. case 4:
  498. bytes_left >>= 1;
  499. case 2:
  500. bytes_left >>= 1;
  501. }
  502. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  503. if (rx_thre > bytes_left)
  504. rx_thre = bytes_left;
  505. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  506. }
  507. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  508. }
  509. /* We did something */
  510. return IRQ_HANDLED;
  511. }
  512. static irqreturn_t ssp_int(int irq, void *dev_id)
  513. {
  514. struct driver_data *drv_data = dev_id;
  515. u32 sccr1_reg;
  516. u32 mask = drv_data->mask_sr;
  517. u32 status;
  518. /*
  519. * The IRQ might be shared with other peripherals so we must first
  520. * check that are we RPM suspended or not. If we are we assume that
  521. * the IRQ was not for us (we shouldn't be RPM suspended when the
  522. * interrupt is enabled).
  523. */
  524. if (pm_runtime_suspended(&drv_data->pdev->dev))
  525. return IRQ_NONE;
  526. /*
  527. * If the device is not yet in RPM suspended state and we get an
  528. * interrupt that is meant for another device, check if status bits
  529. * are all set to one. That means that the device is already
  530. * powered off.
  531. */
  532. status = pxa2xx_spi_read(drv_data, SSSR);
  533. if (status == ~0)
  534. return IRQ_NONE;
  535. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  536. /* Ignore possible writes if we don't need to write */
  537. if (!(sccr1_reg & SSCR1_TIE))
  538. mask &= ~SSSR_TFS;
  539. if (!(status & mask))
  540. return IRQ_NONE;
  541. if (!drv_data->cur_msg) {
  542. pxa2xx_spi_write(drv_data, SSCR0,
  543. pxa2xx_spi_read(drv_data, SSCR0)
  544. & ~SSCR0_SSE);
  545. pxa2xx_spi_write(drv_data, SSCR1,
  546. pxa2xx_spi_read(drv_data, SSCR1)
  547. & ~drv_data->int_cr1);
  548. if (!pxa25x_ssp_comp(drv_data))
  549. pxa2xx_spi_write(drv_data, SSTO, 0);
  550. write_SSSR_CS(drv_data, drv_data->clear_sr);
  551. dev_err(&drv_data->pdev->dev,
  552. "bad message state in interrupt handler\n");
  553. /* Never fail */
  554. return IRQ_HANDLED;
  555. }
  556. return drv_data->transfer_handler(drv_data);
  557. }
  558. /*
  559. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  560. * input frequency by fractions of 2^24. It also has a divider by 5.
  561. *
  562. * There are formulas to get baud rate value for given input frequency and
  563. * divider parameters, such as DDS_CLK_RATE and SCR:
  564. *
  565. * Fsys = 200MHz
  566. *
  567. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  568. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  569. *
  570. * DDS_CLK_RATE either 2^n or 2^n / 5.
  571. * SCR is in range 0 .. 255
  572. *
  573. * Divisor = 5^i * 2^j * 2 * k
  574. * i = [0, 1] i = 1 iff j = 0 or j > 3
  575. * j = [0, 23] j = 0 iff i = 1
  576. * k = [1, 256]
  577. * Special case: j = 0, i = 1: Divisor = 2 / 5
  578. *
  579. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  580. * are:
  581. * Case 1: 2^n, n = [0, 23]
  582. * Case 2: 2^24 * 2 / 5 (0x666666)
  583. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  584. *
  585. * In all cases the lowest possible value is better.
  586. *
  587. * The function calculates parameters for all cases and chooses the one closest
  588. * to the asked baud rate.
  589. */
  590. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  591. {
  592. unsigned long xtal = 200000000;
  593. unsigned long fref = xtal / 2; /* mandatory division by 2,
  594. see (2) */
  595. /* case 3 */
  596. unsigned long fref1 = fref / 2; /* case 1 */
  597. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  598. unsigned long scale;
  599. unsigned long q, q1, q2;
  600. long r, r1, r2;
  601. u32 mul;
  602. /* Case 1 */
  603. /* Set initial value for DDS_CLK_RATE */
  604. mul = (1 << 24) >> 1;
  605. /* Calculate initial quot */
  606. q1 = DIV_ROUND_CLOSEST(fref1, rate);
  607. /* Scale q1 if it's too big */
  608. if (q1 > 256) {
  609. /* Scale q1 to range [1, 512] */
  610. scale = fls_long(q1 - 1);
  611. if (scale > 9) {
  612. q1 >>= scale - 9;
  613. mul >>= scale - 9;
  614. }
  615. /* Round the result if we have a remainder */
  616. q1 += q1 & 1;
  617. }
  618. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  619. scale = __ffs(q1);
  620. q1 >>= scale;
  621. mul >>= scale;
  622. /* Get the remainder */
  623. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  624. /* Case 2 */
  625. q2 = DIV_ROUND_CLOSEST(fref2, rate);
  626. r2 = abs(fref2 / q2 - rate);
  627. /*
  628. * Choose the best between two: less remainder we have the better. We
  629. * can't go case 2 if q2 is greater than 256 since SCR register can
  630. * hold only values 0 .. 255.
  631. */
  632. if (r2 >= r1 || q2 > 256) {
  633. /* case 1 is better */
  634. r = r1;
  635. q = q1;
  636. } else {
  637. /* case 2 is better */
  638. r = r2;
  639. q = q2;
  640. mul = (1 << 24) * 2 / 5;
  641. }
  642. /* Check case 3 only If the divisor is big enough */
  643. if (fref / rate >= 80) {
  644. u64 fssp;
  645. u32 m;
  646. /* Calculate initial quot */
  647. q1 = DIV_ROUND_CLOSEST(fref, rate);
  648. m = (1 << 24) / q1;
  649. /* Get the remainder */
  650. fssp = (u64)fref * m;
  651. do_div(fssp, 1 << 24);
  652. r1 = abs(fssp - rate);
  653. /* Choose this one if it suits better */
  654. if (r1 < r) {
  655. /* case 3 is better */
  656. q = 1;
  657. mul = m;
  658. }
  659. }
  660. *dds = mul;
  661. return q - 1;
  662. }
  663. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  664. {
  665. unsigned long ssp_clk = drv_data->max_clk_rate;
  666. const struct ssp_device *ssp = drv_data->ssp;
  667. rate = min_t(int, ssp_clk, rate);
  668. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  669. return (ssp_clk / (2 * rate) - 1) & 0xff;
  670. else
  671. return (ssp_clk / rate - 1) & 0xfff;
  672. }
  673. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  674. struct chip_data *chip, int rate)
  675. {
  676. unsigned int clk_div;
  677. switch (drv_data->ssp_type) {
  678. case QUARK_X1000_SSP:
  679. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  680. break;
  681. default:
  682. clk_div = ssp_get_clk_div(drv_data, rate);
  683. break;
  684. }
  685. return clk_div << 8;
  686. }
  687. static void pump_transfers(unsigned long data)
  688. {
  689. struct driver_data *drv_data = (struct driver_data *)data;
  690. struct spi_message *message = NULL;
  691. struct spi_transfer *transfer = NULL;
  692. struct spi_transfer *previous = NULL;
  693. struct chip_data *chip = NULL;
  694. u32 clk_div = 0;
  695. u8 bits = 0;
  696. u32 speed = 0;
  697. u32 cr0;
  698. u32 cr1;
  699. u32 dma_thresh = drv_data->cur_chip->dma_threshold;
  700. u32 dma_burst = drv_data->cur_chip->dma_burst_size;
  701. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  702. /* Get current state information */
  703. message = drv_data->cur_msg;
  704. transfer = drv_data->cur_transfer;
  705. chip = drv_data->cur_chip;
  706. /* Handle for abort */
  707. if (message->state == ERROR_STATE) {
  708. message->status = -EIO;
  709. giveback(drv_data);
  710. return;
  711. }
  712. /* Handle end of message */
  713. if (message->state == DONE_STATE) {
  714. message->status = 0;
  715. giveback(drv_data);
  716. return;
  717. }
  718. /* Delay if requested at end of transfer before CS change */
  719. if (message->state == RUNNING_STATE) {
  720. previous = list_entry(transfer->transfer_list.prev,
  721. struct spi_transfer,
  722. transfer_list);
  723. if (previous->delay_usecs)
  724. udelay(previous->delay_usecs);
  725. /* Drop chip select only if cs_change is requested */
  726. if (previous->cs_change)
  727. cs_deassert(drv_data);
  728. }
  729. /* Check if we can DMA this transfer */
  730. if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
  731. /* reject already-mapped transfers; PIO won't always work */
  732. if (message->is_dma_mapped
  733. || transfer->rx_dma || transfer->tx_dma) {
  734. dev_err(&drv_data->pdev->dev,
  735. "pump_transfers: mapped transfer length of "
  736. "%u is greater than %d\n",
  737. transfer->len, MAX_DMA_LEN);
  738. message->status = -EINVAL;
  739. giveback(drv_data);
  740. return;
  741. }
  742. /* warn ... we force this to PIO mode */
  743. dev_warn_ratelimited(&message->spi->dev,
  744. "pump_transfers: DMA disabled for transfer length %ld "
  745. "greater than %d\n",
  746. (long)drv_data->len, MAX_DMA_LEN);
  747. }
  748. /* Setup the transfer state based on the type of transfer */
  749. if (pxa2xx_spi_flush(drv_data) == 0) {
  750. dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
  751. message->status = -EIO;
  752. giveback(drv_data);
  753. return;
  754. }
  755. drv_data->n_bytes = chip->n_bytes;
  756. drv_data->tx = (void *)transfer->tx_buf;
  757. drv_data->tx_end = drv_data->tx + transfer->len;
  758. drv_data->rx = transfer->rx_buf;
  759. drv_data->rx_end = drv_data->rx + transfer->len;
  760. drv_data->rx_dma = transfer->rx_dma;
  761. drv_data->tx_dma = transfer->tx_dma;
  762. drv_data->len = transfer->len;
  763. drv_data->write = drv_data->tx ? chip->write : null_writer;
  764. drv_data->read = drv_data->rx ? chip->read : null_reader;
  765. /* Change speed and bit per word on a per transfer */
  766. cr0 = chip->cr0;
  767. if (transfer->speed_hz || transfer->bits_per_word) {
  768. bits = chip->bits_per_word;
  769. speed = chip->speed_hz;
  770. if (transfer->speed_hz)
  771. speed = transfer->speed_hz;
  772. if (transfer->bits_per_word)
  773. bits = transfer->bits_per_word;
  774. clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, speed);
  775. if (bits <= 8) {
  776. drv_data->n_bytes = 1;
  777. drv_data->read = drv_data->read != null_reader ?
  778. u8_reader : null_reader;
  779. drv_data->write = drv_data->write != null_writer ?
  780. u8_writer : null_writer;
  781. } else if (bits <= 16) {
  782. drv_data->n_bytes = 2;
  783. drv_data->read = drv_data->read != null_reader ?
  784. u16_reader : null_reader;
  785. drv_data->write = drv_data->write != null_writer ?
  786. u16_writer : null_writer;
  787. } else if (bits <= 32) {
  788. drv_data->n_bytes = 4;
  789. drv_data->read = drv_data->read != null_reader ?
  790. u32_reader : null_reader;
  791. drv_data->write = drv_data->write != null_writer ?
  792. u32_writer : null_writer;
  793. }
  794. /* if bits/word is changed in dma mode, then must check the
  795. * thresholds and burst also */
  796. if (chip->enable_dma) {
  797. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  798. message->spi,
  799. bits, &dma_burst,
  800. &dma_thresh))
  801. dev_warn_ratelimited(&message->spi->dev,
  802. "pump_transfers: DMA burst size reduced to match bits_per_word\n");
  803. }
  804. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  805. }
  806. message->state = RUNNING_STATE;
  807. drv_data->dma_mapped = 0;
  808. if (pxa2xx_spi_dma_is_possible(drv_data->len))
  809. drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
  810. if (drv_data->dma_mapped) {
  811. /* Ensure we have the correct interrupt handler */
  812. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  813. pxa2xx_spi_dma_prepare(drv_data, dma_burst);
  814. /* Clear status and start DMA engine */
  815. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  816. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  817. pxa2xx_spi_dma_start(drv_data);
  818. } else {
  819. /* Ensure we have the correct interrupt handler */
  820. drv_data->transfer_handler = interrupt_transfer;
  821. /* Clear status */
  822. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  823. write_SSSR_CS(drv_data, drv_data->clear_sr);
  824. }
  825. if (is_lpss_ssp(drv_data)) {
  826. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  827. != chip->lpss_rx_threshold)
  828. pxa2xx_spi_write(drv_data, SSIRF,
  829. chip->lpss_rx_threshold);
  830. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  831. != chip->lpss_tx_threshold)
  832. pxa2xx_spi_write(drv_data, SSITF,
  833. chip->lpss_tx_threshold);
  834. }
  835. if (is_quark_x1000_ssp(drv_data) &&
  836. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  837. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  838. /* see if we need to reload the config registers */
  839. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  840. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  841. != (cr1 & change_mask)) {
  842. /* stop the SSP, and update the other bits */
  843. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  844. if (!pxa25x_ssp_comp(drv_data))
  845. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  846. /* first set CR1 without interrupt and service enables */
  847. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  848. /* restart the SSP */
  849. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  850. } else {
  851. if (!pxa25x_ssp_comp(drv_data))
  852. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  853. }
  854. cs_assert(drv_data);
  855. /* after chip select, release the data by enabling service
  856. * requests and interrupts, without changing any mode bits */
  857. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  858. }
  859. static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
  860. struct spi_message *msg)
  861. {
  862. struct driver_data *drv_data = spi_master_get_devdata(master);
  863. drv_data->cur_msg = msg;
  864. /* Initial message state*/
  865. drv_data->cur_msg->state = START_STATE;
  866. drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
  867. struct spi_transfer,
  868. transfer_list);
  869. /* prepare to setup the SSP, in pump_transfers, using the per
  870. * chip configuration */
  871. drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
  872. /* Mark as busy and launch transfers */
  873. tasklet_schedule(&drv_data->pump_transfers);
  874. return 0;
  875. }
  876. static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
  877. {
  878. struct driver_data *drv_data = spi_master_get_devdata(master);
  879. /* Disable the SSP now */
  880. pxa2xx_spi_write(drv_data, SSCR0,
  881. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  882. return 0;
  883. }
  884. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  885. struct pxa2xx_spi_chip *chip_info)
  886. {
  887. int err = 0;
  888. if (chip == NULL || chip_info == NULL)
  889. return 0;
  890. /* NOTE: setup() can be called multiple times, possibly with
  891. * different chip_info, release previously requested GPIO
  892. */
  893. if (gpio_is_valid(chip->gpio_cs))
  894. gpio_free(chip->gpio_cs);
  895. /* If (*cs_control) is provided, ignore GPIO chip select */
  896. if (chip_info->cs_control) {
  897. chip->cs_control = chip_info->cs_control;
  898. return 0;
  899. }
  900. if (gpio_is_valid(chip_info->gpio_cs)) {
  901. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  902. if (err) {
  903. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  904. chip_info->gpio_cs);
  905. return err;
  906. }
  907. chip->gpio_cs = chip_info->gpio_cs;
  908. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  909. err = gpio_direction_output(chip->gpio_cs,
  910. !chip->gpio_cs_inverted);
  911. }
  912. return err;
  913. }
  914. static int setup(struct spi_device *spi)
  915. {
  916. struct pxa2xx_spi_chip *chip_info = NULL;
  917. struct chip_data *chip;
  918. const struct lpss_config *config;
  919. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  920. unsigned int clk_div;
  921. uint tx_thres, tx_hi_thres, rx_thres;
  922. switch (drv_data->ssp_type) {
  923. case QUARK_X1000_SSP:
  924. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  925. tx_hi_thres = 0;
  926. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  927. break;
  928. case LPSS_LPT_SSP:
  929. case LPSS_BYT_SSP:
  930. config = lpss_get_config(drv_data);
  931. tx_thres = config->tx_threshold_lo;
  932. tx_hi_thres = config->tx_threshold_hi;
  933. rx_thres = config->rx_threshold;
  934. break;
  935. default:
  936. tx_thres = TX_THRESH_DFLT;
  937. tx_hi_thres = 0;
  938. rx_thres = RX_THRESH_DFLT;
  939. break;
  940. }
  941. /* Only alloc on first setup */
  942. chip = spi_get_ctldata(spi);
  943. if (!chip) {
  944. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  945. if (!chip)
  946. return -ENOMEM;
  947. if (drv_data->ssp_type == CE4100_SSP) {
  948. if (spi->chip_select > 4) {
  949. dev_err(&spi->dev,
  950. "failed setup: cs number must not be > 4.\n");
  951. kfree(chip);
  952. return -EINVAL;
  953. }
  954. chip->frm = spi->chip_select;
  955. } else
  956. chip->gpio_cs = -1;
  957. chip->enable_dma = 0;
  958. chip->timeout = TIMOUT_DFLT;
  959. }
  960. /* protocol drivers may change the chip settings, so...
  961. * if chip_info exists, use it */
  962. chip_info = spi->controller_data;
  963. /* chip_info isn't always needed */
  964. chip->cr1 = 0;
  965. if (chip_info) {
  966. if (chip_info->timeout)
  967. chip->timeout = chip_info->timeout;
  968. if (chip_info->tx_threshold)
  969. tx_thres = chip_info->tx_threshold;
  970. if (chip_info->tx_hi_threshold)
  971. tx_hi_thres = chip_info->tx_hi_threshold;
  972. if (chip_info->rx_threshold)
  973. rx_thres = chip_info->rx_threshold;
  974. chip->enable_dma = drv_data->master_info->enable_dma;
  975. chip->dma_threshold = 0;
  976. if (chip_info->enable_loopback)
  977. chip->cr1 = SSCR1_LBM;
  978. } else if (ACPI_HANDLE(&spi->dev)) {
  979. /*
  980. * Slave devices enumerated from ACPI namespace don't
  981. * usually have chip_info but we still might want to use
  982. * DMA with them.
  983. */
  984. chip->enable_dma = drv_data->master_info->enable_dma;
  985. }
  986. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  987. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  988. | SSITF_TxHiThresh(tx_hi_thres);
  989. /* set dma burst and threshold outside of chip_info path so that if
  990. * chip_info goes away after setting chip->enable_dma, the
  991. * burst and threshold can still respond to changes in bits_per_word */
  992. if (chip->enable_dma) {
  993. /* set up legal burst and threshold for dma */
  994. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  995. spi->bits_per_word,
  996. &chip->dma_burst_size,
  997. &chip->dma_threshold)) {
  998. dev_warn(&spi->dev,
  999. "in setup: DMA burst size reduced to match bits_per_word\n");
  1000. }
  1001. }
  1002. clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, spi->max_speed_hz);
  1003. chip->speed_hz = spi->max_speed_hz;
  1004. chip->cr0 = pxa2xx_configure_sscr0(drv_data, clk_div,
  1005. spi->bits_per_word);
  1006. switch (drv_data->ssp_type) {
  1007. case QUARK_X1000_SSP:
  1008. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1009. & QUARK_X1000_SSCR1_RFT)
  1010. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1011. & QUARK_X1000_SSCR1_TFT);
  1012. break;
  1013. default:
  1014. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1015. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1016. break;
  1017. }
  1018. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1019. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1020. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1021. if (spi->mode & SPI_LOOP)
  1022. chip->cr1 |= SSCR1_LBM;
  1023. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  1024. if (!pxa25x_ssp_comp(drv_data))
  1025. dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
  1026. drv_data->max_clk_rate
  1027. / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
  1028. chip->enable_dma ? "DMA" : "PIO");
  1029. else
  1030. dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
  1031. drv_data->max_clk_rate / 2
  1032. / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  1033. chip->enable_dma ? "DMA" : "PIO");
  1034. if (spi->bits_per_word <= 8) {
  1035. chip->n_bytes = 1;
  1036. chip->read = u8_reader;
  1037. chip->write = u8_writer;
  1038. } else if (spi->bits_per_word <= 16) {
  1039. chip->n_bytes = 2;
  1040. chip->read = u16_reader;
  1041. chip->write = u16_writer;
  1042. } else if (spi->bits_per_word <= 32) {
  1043. if (!is_quark_x1000_ssp(drv_data))
  1044. chip->cr0 |= SSCR0_EDSS;
  1045. chip->n_bytes = 4;
  1046. chip->read = u32_reader;
  1047. chip->write = u32_writer;
  1048. }
  1049. chip->bits_per_word = spi->bits_per_word;
  1050. spi_set_ctldata(spi, chip);
  1051. if (drv_data->ssp_type == CE4100_SSP)
  1052. return 0;
  1053. return setup_cs(spi, chip, chip_info);
  1054. }
  1055. static void cleanup(struct spi_device *spi)
  1056. {
  1057. struct chip_data *chip = spi_get_ctldata(spi);
  1058. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1059. if (!chip)
  1060. return;
  1061. if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
  1062. gpio_free(chip->gpio_cs);
  1063. kfree(chip);
  1064. }
  1065. #ifdef CONFIG_ACPI
  1066. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1067. { "INT33C0", LPSS_LPT_SSP },
  1068. { "INT33C1", LPSS_LPT_SSP },
  1069. { "INT3430", LPSS_LPT_SSP },
  1070. { "INT3431", LPSS_LPT_SSP },
  1071. { "80860F0E", LPSS_BYT_SSP },
  1072. { "8086228E", LPSS_BYT_SSP },
  1073. { },
  1074. };
  1075. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1076. static struct pxa2xx_spi_master *
  1077. pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
  1078. {
  1079. struct pxa2xx_spi_master *pdata;
  1080. struct acpi_device *adev;
  1081. struct ssp_device *ssp;
  1082. struct resource *res;
  1083. const struct acpi_device_id *id;
  1084. int devid, type;
  1085. if (!ACPI_HANDLE(&pdev->dev) ||
  1086. acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
  1087. return NULL;
  1088. id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
  1089. if (id)
  1090. type = (int)id->driver_data;
  1091. else
  1092. return NULL;
  1093. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1094. if (!pdata)
  1095. return NULL;
  1096. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1097. if (!res)
  1098. return NULL;
  1099. ssp = &pdata->ssp;
  1100. ssp->phys_base = res->start;
  1101. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1102. if (IS_ERR(ssp->mmio_base))
  1103. return NULL;
  1104. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1105. ssp->irq = platform_get_irq(pdev, 0);
  1106. ssp->type = type;
  1107. ssp->pdev = pdev;
  1108. ssp->port_id = -1;
  1109. if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid))
  1110. ssp->port_id = devid;
  1111. pdata->num_chipselect = 1;
  1112. pdata->enable_dma = true;
  1113. return pdata;
  1114. }
  1115. #else
  1116. static inline struct pxa2xx_spi_master *
  1117. pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
  1118. {
  1119. return NULL;
  1120. }
  1121. #endif
  1122. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1123. {
  1124. struct device *dev = &pdev->dev;
  1125. struct pxa2xx_spi_master *platform_info;
  1126. struct spi_master *master;
  1127. struct driver_data *drv_data;
  1128. struct ssp_device *ssp;
  1129. int status;
  1130. u32 tmp;
  1131. platform_info = dev_get_platdata(dev);
  1132. if (!platform_info) {
  1133. platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
  1134. if (!platform_info) {
  1135. dev_err(&pdev->dev, "missing platform data\n");
  1136. return -ENODEV;
  1137. }
  1138. }
  1139. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1140. if (!ssp)
  1141. ssp = &platform_info->ssp;
  1142. if (!ssp->mmio_base) {
  1143. dev_err(&pdev->dev, "failed to get ssp\n");
  1144. return -ENODEV;
  1145. }
  1146. /* Allocate master with space for drv_data and null dma buffer */
  1147. master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
  1148. if (!master) {
  1149. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1150. pxa_ssp_free(ssp);
  1151. return -ENOMEM;
  1152. }
  1153. drv_data = spi_master_get_devdata(master);
  1154. drv_data->master = master;
  1155. drv_data->master_info = platform_info;
  1156. drv_data->pdev = pdev;
  1157. drv_data->ssp = ssp;
  1158. master->dev.parent = &pdev->dev;
  1159. master->dev.of_node = pdev->dev.of_node;
  1160. /* the spi->mode bits understood by this driver: */
  1161. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1162. master->bus_num = ssp->port_id;
  1163. master->num_chipselect = platform_info->num_chipselect;
  1164. master->dma_alignment = DMA_ALIGNMENT;
  1165. master->cleanup = cleanup;
  1166. master->setup = setup;
  1167. master->transfer_one_message = pxa2xx_spi_transfer_one_message;
  1168. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1169. master->auto_runtime_pm = true;
  1170. drv_data->ssp_type = ssp->type;
  1171. drv_data->null_dma_buf = (u32 *)PTR_ALIGN(&drv_data[1], DMA_ALIGNMENT);
  1172. drv_data->ioaddr = ssp->mmio_base;
  1173. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1174. if (pxa25x_ssp_comp(drv_data)) {
  1175. switch (drv_data->ssp_type) {
  1176. case QUARK_X1000_SSP:
  1177. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1178. break;
  1179. default:
  1180. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1181. break;
  1182. }
  1183. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1184. drv_data->dma_cr1 = 0;
  1185. drv_data->clear_sr = SSSR_ROR;
  1186. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1187. } else {
  1188. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1189. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1190. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1191. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1192. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1193. }
  1194. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1195. drv_data);
  1196. if (status < 0) {
  1197. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1198. goto out_error_master_alloc;
  1199. }
  1200. /* Setup DMA if requested */
  1201. drv_data->tx_channel = -1;
  1202. drv_data->rx_channel = -1;
  1203. if (platform_info->enable_dma) {
  1204. status = pxa2xx_spi_dma_setup(drv_data);
  1205. if (status) {
  1206. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1207. platform_info->enable_dma = false;
  1208. }
  1209. }
  1210. /* Enable SOC clock */
  1211. clk_prepare_enable(ssp->clk);
  1212. drv_data->max_clk_rate = clk_get_rate(ssp->clk);
  1213. /* Load default SSP configuration */
  1214. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1215. switch (drv_data->ssp_type) {
  1216. case QUARK_X1000_SSP:
  1217. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
  1218. | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1219. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1220. /* using the Motorola SPI protocol and use 8 bit frame */
  1221. pxa2xx_spi_write(drv_data, SSCR0,
  1222. QUARK_X1000_SSCR0_Motorola
  1223. | QUARK_X1000_SSCR0_DataSize(8));
  1224. break;
  1225. default:
  1226. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1227. SSCR1_TxTresh(TX_THRESH_DFLT);
  1228. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1229. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1230. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1231. break;
  1232. }
  1233. if (!pxa25x_ssp_comp(drv_data))
  1234. pxa2xx_spi_write(drv_data, SSTO, 0);
  1235. if (!is_quark_x1000_ssp(drv_data))
  1236. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1237. if (is_lpss_ssp(drv_data))
  1238. lpss_ssp_setup(drv_data);
  1239. tasklet_init(&drv_data->pump_transfers, pump_transfers,
  1240. (unsigned long)drv_data);
  1241. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1242. pm_runtime_use_autosuspend(&pdev->dev);
  1243. pm_runtime_set_active(&pdev->dev);
  1244. pm_runtime_enable(&pdev->dev);
  1245. /* Register with the SPI framework */
  1246. platform_set_drvdata(pdev, drv_data);
  1247. status = devm_spi_register_master(&pdev->dev, master);
  1248. if (status != 0) {
  1249. dev_err(&pdev->dev, "problem registering spi master\n");
  1250. goto out_error_clock_enabled;
  1251. }
  1252. return status;
  1253. out_error_clock_enabled:
  1254. clk_disable_unprepare(ssp->clk);
  1255. pxa2xx_spi_dma_release(drv_data);
  1256. free_irq(ssp->irq, drv_data);
  1257. out_error_master_alloc:
  1258. spi_master_put(master);
  1259. pxa_ssp_free(ssp);
  1260. return status;
  1261. }
  1262. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1263. {
  1264. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1265. struct ssp_device *ssp;
  1266. if (!drv_data)
  1267. return 0;
  1268. ssp = drv_data->ssp;
  1269. pm_runtime_get_sync(&pdev->dev);
  1270. /* Disable the SSP at the peripheral and SOC level */
  1271. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1272. clk_disable_unprepare(ssp->clk);
  1273. /* Release DMA */
  1274. if (drv_data->master_info->enable_dma)
  1275. pxa2xx_spi_dma_release(drv_data);
  1276. pm_runtime_put_noidle(&pdev->dev);
  1277. pm_runtime_disable(&pdev->dev);
  1278. /* Release IRQ */
  1279. free_irq(ssp->irq, drv_data);
  1280. /* Release SSP */
  1281. pxa_ssp_free(ssp);
  1282. return 0;
  1283. }
  1284. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1285. {
  1286. int status = 0;
  1287. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1288. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1289. }
  1290. #ifdef CONFIG_PM_SLEEP
  1291. static int pxa2xx_spi_suspend(struct device *dev)
  1292. {
  1293. struct driver_data *drv_data = dev_get_drvdata(dev);
  1294. struct ssp_device *ssp = drv_data->ssp;
  1295. int status = 0;
  1296. status = spi_master_suspend(drv_data->master);
  1297. if (status != 0)
  1298. return status;
  1299. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1300. if (!pm_runtime_suspended(dev))
  1301. clk_disable_unprepare(ssp->clk);
  1302. return 0;
  1303. }
  1304. static int pxa2xx_spi_resume(struct device *dev)
  1305. {
  1306. struct driver_data *drv_data = dev_get_drvdata(dev);
  1307. struct ssp_device *ssp = drv_data->ssp;
  1308. int status = 0;
  1309. pxa2xx_spi_dma_resume(drv_data);
  1310. /* Enable the SSP clock */
  1311. if (!pm_runtime_suspended(dev))
  1312. clk_prepare_enable(ssp->clk);
  1313. /* Restore LPSS private register bits */
  1314. if (is_lpss_ssp(drv_data))
  1315. lpss_ssp_setup(drv_data);
  1316. /* Start the queue running */
  1317. status = spi_master_resume(drv_data->master);
  1318. if (status != 0) {
  1319. dev_err(dev, "problem starting queue (%d)\n", status);
  1320. return status;
  1321. }
  1322. return 0;
  1323. }
  1324. #endif
  1325. #ifdef CONFIG_PM
  1326. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1327. {
  1328. struct driver_data *drv_data = dev_get_drvdata(dev);
  1329. clk_disable_unprepare(drv_data->ssp->clk);
  1330. return 0;
  1331. }
  1332. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1333. {
  1334. struct driver_data *drv_data = dev_get_drvdata(dev);
  1335. clk_prepare_enable(drv_data->ssp->clk);
  1336. return 0;
  1337. }
  1338. #endif
  1339. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1340. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1341. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1342. pxa2xx_spi_runtime_resume, NULL)
  1343. };
  1344. static struct platform_driver driver = {
  1345. .driver = {
  1346. .name = "pxa2xx-spi",
  1347. .pm = &pxa2xx_spi_pm_ops,
  1348. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1349. },
  1350. .probe = pxa2xx_spi_probe,
  1351. .remove = pxa2xx_spi_remove,
  1352. .shutdown = pxa2xx_spi_shutdown,
  1353. };
  1354. static int __init pxa2xx_spi_init(void)
  1355. {
  1356. return platform_driver_register(&driver);
  1357. }
  1358. subsys_initcall(pxa2xx_spi_init);
  1359. static void __exit pxa2xx_spi_exit(void)
  1360. {
  1361. platform_driver_unregister(&driver);
  1362. }
  1363. module_exit(pxa2xx_spi_exit);