pcmcia.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * Sonics Silicon Backplane
  3. * PCMCIA-Hostbus related functions
  4. *
  5. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2007-2008 Michael Buesch <m@bues.ch>
  7. *
  8. * Licensed under the GNU/GPL. See COPYING for details.
  9. */
  10. #include "ssb_private.h"
  11. #include <linux/ssb/ssb.h>
  12. #include <linux/delay.h>
  13. #include <linux/io.h>
  14. #include <linux/etherdevice.h>
  15. #include <pcmcia/cistpl.h>
  16. #include <pcmcia/ciscode.h>
  17. #include <pcmcia/ds.h>
  18. #include <pcmcia/cisreg.h>
  19. /* Define the following to 1 to enable a printk on each coreswitch. */
  20. #define SSB_VERBOSE_PCMCIACORESWITCH_DEBUG 0
  21. /* PCMCIA configuration registers */
  22. #define SSB_PCMCIA_ADDRESS0 0x2E
  23. #define SSB_PCMCIA_ADDRESS1 0x30
  24. #define SSB_PCMCIA_ADDRESS2 0x32
  25. #define SSB_PCMCIA_MEMSEG 0x34
  26. #define SSB_PCMCIA_SPROMCTL 0x36
  27. #define SSB_PCMCIA_SPROMCTL_IDLE 0
  28. #define SSB_PCMCIA_SPROMCTL_WRITE 1
  29. #define SSB_PCMCIA_SPROMCTL_READ 2
  30. #define SSB_PCMCIA_SPROMCTL_WRITEEN 4
  31. #define SSB_PCMCIA_SPROMCTL_WRITEDIS 7
  32. #define SSB_PCMCIA_SPROMCTL_DONE 8
  33. #define SSB_PCMCIA_SPROM_DATALO 0x38
  34. #define SSB_PCMCIA_SPROM_DATAHI 0x3A
  35. #define SSB_PCMCIA_SPROM_ADDRLO 0x3C
  36. #define SSB_PCMCIA_SPROM_ADDRHI 0x3E
  37. /* Hardware invariants CIS tuples */
  38. #define SSB_PCMCIA_CIS 0x80
  39. #define SSB_PCMCIA_CIS_ID 0x01
  40. #define SSB_PCMCIA_CIS_BOARDREV 0x02
  41. #define SSB_PCMCIA_CIS_PA 0x03
  42. #define SSB_PCMCIA_CIS_PA_PA0B0_LO 0
  43. #define SSB_PCMCIA_CIS_PA_PA0B0_HI 1
  44. #define SSB_PCMCIA_CIS_PA_PA0B1_LO 2
  45. #define SSB_PCMCIA_CIS_PA_PA0B1_HI 3
  46. #define SSB_PCMCIA_CIS_PA_PA0B2_LO 4
  47. #define SSB_PCMCIA_CIS_PA_PA0B2_HI 5
  48. #define SSB_PCMCIA_CIS_PA_ITSSI 6
  49. #define SSB_PCMCIA_CIS_PA_MAXPOW 7
  50. #define SSB_PCMCIA_CIS_OEMNAME 0x04
  51. #define SSB_PCMCIA_CIS_CCODE 0x05
  52. #define SSB_PCMCIA_CIS_ANTENNA 0x06
  53. #define SSB_PCMCIA_CIS_ANTGAIN 0x07
  54. #define SSB_PCMCIA_CIS_BFLAGS 0x08
  55. #define SSB_PCMCIA_CIS_LEDS 0x09
  56. /* PCMCIA SPROM size. */
  57. #define SSB_PCMCIA_SPROM_SIZE 256
  58. #define SSB_PCMCIA_SPROM_SIZE_BYTES (SSB_PCMCIA_SPROM_SIZE * sizeof(u16))
  59. /* Write to a PCMCIA configuration register. */
  60. static int ssb_pcmcia_cfg_write(struct ssb_bus *bus, u8 offset, u8 value)
  61. {
  62. int res;
  63. res = pcmcia_write_config_byte(bus->host_pcmcia, offset, value);
  64. if (unlikely(res != 0))
  65. return -EBUSY;
  66. return 0;
  67. }
  68. /* Read from a PCMCIA configuration register. */
  69. static int ssb_pcmcia_cfg_read(struct ssb_bus *bus, u8 offset, u8 *value)
  70. {
  71. int res;
  72. res = pcmcia_read_config_byte(bus->host_pcmcia, offset, value);
  73. if (unlikely(res != 0))
  74. return -EBUSY;
  75. return 0;
  76. }
  77. int ssb_pcmcia_switch_coreidx(struct ssb_bus *bus,
  78. u8 coreidx)
  79. {
  80. int err;
  81. int attempts = 0;
  82. u32 cur_core;
  83. u32 addr;
  84. u32 read_addr;
  85. u8 val;
  86. addr = (coreidx * SSB_CORE_SIZE) + SSB_ENUM_BASE;
  87. while (1) {
  88. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS0,
  89. (addr & 0x0000F000) >> 12);
  90. if (err)
  91. goto error;
  92. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS1,
  93. (addr & 0x00FF0000) >> 16);
  94. if (err)
  95. goto error;
  96. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS2,
  97. (addr & 0xFF000000) >> 24);
  98. if (err)
  99. goto error;
  100. read_addr = 0;
  101. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS0, &val);
  102. if (err)
  103. goto error;
  104. read_addr |= ((u32)(val & 0x0F)) << 12;
  105. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS1, &val);
  106. if (err)
  107. goto error;
  108. read_addr |= ((u32)val) << 16;
  109. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS2, &val);
  110. if (err)
  111. goto error;
  112. read_addr |= ((u32)val) << 24;
  113. cur_core = (read_addr - SSB_ENUM_BASE) / SSB_CORE_SIZE;
  114. if (cur_core == coreidx)
  115. break;
  116. err = -ETIMEDOUT;
  117. if (attempts++ > SSB_BAR0_MAX_RETRIES)
  118. goto error;
  119. udelay(10);
  120. }
  121. return 0;
  122. error:
  123. pr_err("Failed to switch to core %u\n", coreidx);
  124. return err;
  125. }
  126. static int ssb_pcmcia_switch_core(struct ssb_bus *bus, struct ssb_device *dev)
  127. {
  128. int err;
  129. #if SSB_VERBOSE_PCMCIACORESWITCH_DEBUG
  130. pr_info("Switching to %s core, index %d\n",
  131. ssb_core_name(dev->id.coreid), dev->core_index);
  132. #endif
  133. err = ssb_pcmcia_switch_coreidx(bus, dev->core_index);
  134. if (!err)
  135. bus->mapped_device = dev;
  136. return err;
  137. }
  138. int ssb_pcmcia_switch_segment(struct ssb_bus *bus, u8 seg)
  139. {
  140. int attempts = 0;
  141. int err;
  142. u8 val;
  143. WARN_ON((seg != 0) && (seg != 1));
  144. while (1) {
  145. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_MEMSEG, seg);
  146. if (err)
  147. goto error;
  148. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_MEMSEG, &val);
  149. if (err)
  150. goto error;
  151. if (val == seg)
  152. break;
  153. err = -ETIMEDOUT;
  154. if (unlikely(attempts++ > SSB_BAR0_MAX_RETRIES))
  155. goto error;
  156. udelay(10);
  157. }
  158. bus->mapped_pcmcia_seg = seg;
  159. return 0;
  160. error:
  161. pr_err("Failed to switch pcmcia segment\n");
  162. return err;
  163. }
  164. static int select_core_and_segment(struct ssb_device *dev,
  165. u16 *offset)
  166. {
  167. struct ssb_bus *bus = dev->bus;
  168. int err;
  169. u8 need_segment;
  170. if (*offset >= 0x800) {
  171. *offset -= 0x800;
  172. need_segment = 1;
  173. } else
  174. need_segment = 0;
  175. if (unlikely(dev != bus->mapped_device)) {
  176. err = ssb_pcmcia_switch_core(bus, dev);
  177. if (unlikely(err))
  178. return err;
  179. }
  180. if (unlikely(need_segment != bus->mapped_pcmcia_seg)) {
  181. err = ssb_pcmcia_switch_segment(bus, need_segment);
  182. if (unlikely(err))
  183. return err;
  184. }
  185. return 0;
  186. }
  187. static u8 ssb_pcmcia_read8(struct ssb_device *dev, u16 offset)
  188. {
  189. struct ssb_bus *bus = dev->bus;
  190. unsigned long flags;
  191. int err;
  192. u8 value = 0xFF;
  193. spin_lock_irqsave(&bus->bar_lock, flags);
  194. err = select_core_and_segment(dev, &offset);
  195. if (likely(!err))
  196. value = readb(bus->mmio + offset);
  197. spin_unlock_irqrestore(&bus->bar_lock, flags);
  198. return value;
  199. }
  200. static u16 ssb_pcmcia_read16(struct ssb_device *dev, u16 offset)
  201. {
  202. struct ssb_bus *bus = dev->bus;
  203. unsigned long flags;
  204. int err;
  205. u16 value = 0xFFFF;
  206. spin_lock_irqsave(&bus->bar_lock, flags);
  207. err = select_core_and_segment(dev, &offset);
  208. if (likely(!err))
  209. value = readw(bus->mmio + offset);
  210. spin_unlock_irqrestore(&bus->bar_lock, flags);
  211. return value;
  212. }
  213. static u32 ssb_pcmcia_read32(struct ssb_device *dev, u16 offset)
  214. {
  215. struct ssb_bus *bus = dev->bus;
  216. unsigned long flags;
  217. int err;
  218. u32 lo = 0xFFFFFFFF, hi = 0xFFFFFFFF;
  219. spin_lock_irqsave(&bus->bar_lock, flags);
  220. err = select_core_and_segment(dev, &offset);
  221. if (likely(!err)) {
  222. lo = readw(bus->mmio + offset);
  223. hi = readw(bus->mmio + offset + 2);
  224. }
  225. spin_unlock_irqrestore(&bus->bar_lock, flags);
  226. return (lo | (hi << 16));
  227. }
  228. #ifdef CONFIG_SSB_BLOCKIO
  229. static void ssb_pcmcia_block_read(struct ssb_device *dev, void *buffer,
  230. size_t count, u16 offset, u8 reg_width)
  231. {
  232. struct ssb_bus *bus = dev->bus;
  233. unsigned long flags;
  234. void __iomem *addr = bus->mmio + offset;
  235. int err;
  236. spin_lock_irqsave(&bus->bar_lock, flags);
  237. err = select_core_and_segment(dev, &offset);
  238. if (unlikely(err)) {
  239. memset(buffer, 0xFF, count);
  240. goto unlock;
  241. }
  242. switch (reg_width) {
  243. case sizeof(u8): {
  244. u8 *buf = buffer;
  245. while (count) {
  246. *buf = __raw_readb(addr);
  247. buf++;
  248. count--;
  249. }
  250. break;
  251. }
  252. case sizeof(u16): {
  253. __le16 *buf = buffer;
  254. WARN_ON(count & 1);
  255. while (count) {
  256. *buf = (__force __le16)__raw_readw(addr);
  257. buf++;
  258. count -= 2;
  259. }
  260. break;
  261. }
  262. case sizeof(u32): {
  263. __le16 *buf = buffer;
  264. WARN_ON(count & 3);
  265. while (count) {
  266. *buf = (__force __le16)__raw_readw(addr);
  267. buf++;
  268. *buf = (__force __le16)__raw_readw(addr + 2);
  269. buf++;
  270. count -= 4;
  271. }
  272. break;
  273. }
  274. default:
  275. WARN_ON(1);
  276. }
  277. unlock:
  278. spin_unlock_irqrestore(&bus->bar_lock, flags);
  279. }
  280. #endif /* CONFIG_SSB_BLOCKIO */
  281. static void ssb_pcmcia_write8(struct ssb_device *dev, u16 offset, u8 value)
  282. {
  283. struct ssb_bus *bus = dev->bus;
  284. unsigned long flags;
  285. int err;
  286. spin_lock_irqsave(&bus->bar_lock, flags);
  287. err = select_core_and_segment(dev, &offset);
  288. if (likely(!err))
  289. writeb(value, bus->mmio + offset);
  290. mmiowb();
  291. spin_unlock_irqrestore(&bus->bar_lock, flags);
  292. }
  293. static void ssb_pcmcia_write16(struct ssb_device *dev, u16 offset, u16 value)
  294. {
  295. struct ssb_bus *bus = dev->bus;
  296. unsigned long flags;
  297. int err;
  298. spin_lock_irqsave(&bus->bar_lock, flags);
  299. err = select_core_and_segment(dev, &offset);
  300. if (likely(!err))
  301. writew(value, bus->mmio + offset);
  302. mmiowb();
  303. spin_unlock_irqrestore(&bus->bar_lock, flags);
  304. }
  305. static void ssb_pcmcia_write32(struct ssb_device *dev, u16 offset, u32 value)
  306. {
  307. struct ssb_bus *bus = dev->bus;
  308. unsigned long flags;
  309. int err;
  310. spin_lock_irqsave(&bus->bar_lock, flags);
  311. err = select_core_and_segment(dev, &offset);
  312. if (likely(!err)) {
  313. writew((value & 0x0000FFFF), bus->mmio + offset);
  314. writew(((value & 0xFFFF0000) >> 16), bus->mmio + offset + 2);
  315. }
  316. mmiowb();
  317. spin_unlock_irqrestore(&bus->bar_lock, flags);
  318. }
  319. #ifdef CONFIG_SSB_BLOCKIO
  320. static void ssb_pcmcia_block_write(struct ssb_device *dev, const void *buffer,
  321. size_t count, u16 offset, u8 reg_width)
  322. {
  323. struct ssb_bus *bus = dev->bus;
  324. unsigned long flags;
  325. void __iomem *addr = bus->mmio + offset;
  326. int err;
  327. spin_lock_irqsave(&bus->bar_lock, flags);
  328. err = select_core_and_segment(dev, &offset);
  329. if (unlikely(err))
  330. goto unlock;
  331. switch (reg_width) {
  332. case sizeof(u8): {
  333. const u8 *buf = buffer;
  334. while (count) {
  335. __raw_writeb(*buf, addr);
  336. buf++;
  337. count--;
  338. }
  339. break;
  340. }
  341. case sizeof(u16): {
  342. const __le16 *buf = buffer;
  343. WARN_ON(count & 1);
  344. while (count) {
  345. __raw_writew((__force u16)(*buf), addr);
  346. buf++;
  347. count -= 2;
  348. }
  349. break;
  350. }
  351. case sizeof(u32): {
  352. const __le16 *buf = buffer;
  353. WARN_ON(count & 3);
  354. while (count) {
  355. __raw_writew((__force u16)(*buf), addr);
  356. buf++;
  357. __raw_writew((__force u16)(*buf), addr + 2);
  358. buf++;
  359. count -= 4;
  360. }
  361. break;
  362. }
  363. default:
  364. WARN_ON(1);
  365. }
  366. unlock:
  367. mmiowb();
  368. spin_unlock_irqrestore(&bus->bar_lock, flags);
  369. }
  370. #endif /* CONFIG_SSB_BLOCKIO */
  371. /* Not "static", as it's used in main.c */
  372. const struct ssb_bus_ops ssb_pcmcia_ops = {
  373. .read8 = ssb_pcmcia_read8,
  374. .read16 = ssb_pcmcia_read16,
  375. .read32 = ssb_pcmcia_read32,
  376. .write8 = ssb_pcmcia_write8,
  377. .write16 = ssb_pcmcia_write16,
  378. .write32 = ssb_pcmcia_write32,
  379. #ifdef CONFIG_SSB_BLOCKIO
  380. .block_read = ssb_pcmcia_block_read,
  381. .block_write = ssb_pcmcia_block_write,
  382. #endif
  383. };
  384. static int ssb_pcmcia_sprom_command(struct ssb_bus *bus, u8 command)
  385. {
  386. unsigned int i;
  387. int err;
  388. u8 value;
  389. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROMCTL, command);
  390. if (err)
  391. return err;
  392. for (i = 0; i < 1000; i++) {
  393. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROMCTL, &value);
  394. if (err)
  395. return err;
  396. if (value & SSB_PCMCIA_SPROMCTL_DONE)
  397. return 0;
  398. udelay(10);
  399. }
  400. return -ETIMEDOUT;
  401. }
  402. /* offset is the 16bit word offset */
  403. static int ssb_pcmcia_sprom_read(struct ssb_bus *bus, u16 offset, u16 *value)
  404. {
  405. int err;
  406. u8 lo, hi;
  407. offset *= 2; /* Make byte offset */
  408. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRLO,
  409. (offset & 0x00FF));
  410. if (err)
  411. return err;
  412. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRHI,
  413. (offset & 0xFF00) >> 8);
  414. if (err)
  415. return err;
  416. err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_READ);
  417. if (err)
  418. return err;
  419. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROM_DATALO, &lo);
  420. if (err)
  421. return err;
  422. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROM_DATAHI, &hi);
  423. if (err)
  424. return err;
  425. *value = (lo | (((u16)hi) << 8));
  426. return 0;
  427. }
  428. /* offset is the 16bit word offset */
  429. static int ssb_pcmcia_sprom_write(struct ssb_bus *bus, u16 offset, u16 value)
  430. {
  431. int err;
  432. offset *= 2; /* Make byte offset */
  433. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRLO,
  434. (offset & 0x00FF));
  435. if (err)
  436. return err;
  437. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRHI,
  438. (offset & 0xFF00) >> 8);
  439. if (err)
  440. return err;
  441. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_DATALO,
  442. (value & 0x00FF));
  443. if (err)
  444. return err;
  445. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_DATAHI,
  446. (value & 0xFF00) >> 8);
  447. if (err)
  448. return err;
  449. err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITE);
  450. if (err)
  451. return err;
  452. msleep(20);
  453. return 0;
  454. }
  455. /* Read the SPROM image. bufsize is in 16bit words. */
  456. static int ssb_pcmcia_sprom_read_all(struct ssb_bus *bus, u16 *sprom)
  457. {
  458. int err, i;
  459. for (i = 0; i < SSB_PCMCIA_SPROM_SIZE; i++) {
  460. err = ssb_pcmcia_sprom_read(bus, i, &sprom[i]);
  461. if (err)
  462. return err;
  463. }
  464. return 0;
  465. }
  466. /* Write the SPROM image. size is in 16bit words. */
  467. static int ssb_pcmcia_sprom_write_all(struct ssb_bus *bus, const u16 *sprom)
  468. {
  469. int i, err;
  470. bool failed = 0;
  471. size_t size = SSB_PCMCIA_SPROM_SIZE;
  472. pr_notice("Writing SPROM. Do NOT turn off the power! Please stand by...\n");
  473. err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITEEN);
  474. if (err) {
  475. pr_notice("Could not enable SPROM write access\n");
  476. return -EBUSY;
  477. }
  478. pr_notice("[ 0%%");
  479. msleep(500);
  480. for (i = 0; i < size; i++) {
  481. if (i == size / 4)
  482. pr_cont("25%%");
  483. else if (i == size / 2)
  484. pr_cont("50%%");
  485. else if (i == (size * 3) / 4)
  486. pr_cont("75%%");
  487. else if (i % 2)
  488. pr_cont(".");
  489. err = ssb_pcmcia_sprom_write(bus, i, sprom[i]);
  490. if (err) {
  491. pr_notice("Failed to write to SPROM\n");
  492. failed = 1;
  493. break;
  494. }
  495. }
  496. err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITEDIS);
  497. if (err) {
  498. pr_notice("Could not disable SPROM write access\n");
  499. failed = 1;
  500. }
  501. msleep(500);
  502. if (!failed) {
  503. pr_cont("100%% ]\n");
  504. pr_notice("SPROM written\n");
  505. }
  506. return failed ? -EBUSY : 0;
  507. }
  508. static int ssb_pcmcia_sprom_check_crc(const u16 *sprom, size_t size)
  509. {
  510. //TODO
  511. return 0;
  512. }
  513. #define GOTO_ERROR_ON(condition, description) do { \
  514. if (unlikely(condition)) { \
  515. error_description = description; \
  516. goto error; \
  517. } \
  518. } while (0)
  519. static int ssb_pcmcia_get_mac(struct pcmcia_device *p_dev,
  520. tuple_t *tuple,
  521. void *priv)
  522. {
  523. struct ssb_sprom *sprom = priv;
  524. if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
  525. return -EINVAL;
  526. if (tuple->TupleDataLen != ETH_ALEN + 2)
  527. return -EINVAL;
  528. if (tuple->TupleData[1] != ETH_ALEN)
  529. return -EINVAL;
  530. memcpy(sprom->il0mac, &tuple->TupleData[2], ETH_ALEN);
  531. return 0;
  532. };
  533. static int ssb_pcmcia_do_get_invariants(struct pcmcia_device *p_dev,
  534. tuple_t *tuple,
  535. void *priv)
  536. {
  537. struct ssb_init_invariants *iv = priv;
  538. struct ssb_sprom *sprom = &iv->sprom;
  539. struct ssb_boardinfo *bi = &iv->boardinfo;
  540. const char *error_description;
  541. GOTO_ERROR_ON(tuple->TupleDataLen < 1, "VEN tpl < 1");
  542. switch (tuple->TupleData[0]) {
  543. case SSB_PCMCIA_CIS_ID:
  544. GOTO_ERROR_ON((tuple->TupleDataLen != 5) &&
  545. (tuple->TupleDataLen != 7),
  546. "id tpl size");
  547. bi->vendor = tuple->TupleData[1] |
  548. ((u16)tuple->TupleData[2] << 8);
  549. break;
  550. case SSB_PCMCIA_CIS_BOARDREV:
  551. GOTO_ERROR_ON(tuple->TupleDataLen != 2,
  552. "boardrev tpl size");
  553. sprom->board_rev = tuple->TupleData[1];
  554. break;
  555. case SSB_PCMCIA_CIS_PA:
  556. GOTO_ERROR_ON((tuple->TupleDataLen != 9) &&
  557. (tuple->TupleDataLen != 10),
  558. "pa tpl size");
  559. sprom->pa0b0 = tuple->TupleData[1] |
  560. ((u16)tuple->TupleData[2] << 8);
  561. sprom->pa0b1 = tuple->TupleData[3] |
  562. ((u16)tuple->TupleData[4] << 8);
  563. sprom->pa0b2 = tuple->TupleData[5] |
  564. ((u16)tuple->TupleData[6] << 8);
  565. sprom->itssi_a = tuple->TupleData[7];
  566. sprom->itssi_bg = tuple->TupleData[7];
  567. sprom->maxpwr_a = tuple->TupleData[8];
  568. sprom->maxpwr_bg = tuple->TupleData[8];
  569. break;
  570. case SSB_PCMCIA_CIS_OEMNAME:
  571. /* We ignore this. */
  572. break;
  573. case SSB_PCMCIA_CIS_CCODE:
  574. GOTO_ERROR_ON(tuple->TupleDataLen != 2,
  575. "ccode tpl size");
  576. sprom->country_code = tuple->TupleData[1];
  577. break;
  578. case SSB_PCMCIA_CIS_ANTENNA:
  579. GOTO_ERROR_ON(tuple->TupleDataLen != 2,
  580. "ant tpl size");
  581. sprom->ant_available_a = tuple->TupleData[1];
  582. sprom->ant_available_bg = tuple->TupleData[1];
  583. break;
  584. case SSB_PCMCIA_CIS_ANTGAIN:
  585. GOTO_ERROR_ON(tuple->TupleDataLen != 2,
  586. "antg tpl size");
  587. sprom->antenna_gain.a0 = tuple->TupleData[1];
  588. sprom->antenna_gain.a1 = tuple->TupleData[1];
  589. sprom->antenna_gain.a2 = tuple->TupleData[1];
  590. sprom->antenna_gain.a3 = tuple->TupleData[1];
  591. break;
  592. case SSB_PCMCIA_CIS_BFLAGS:
  593. GOTO_ERROR_ON((tuple->TupleDataLen != 3) &&
  594. (tuple->TupleDataLen != 5),
  595. "bfl tpl size");
  596. sprom->boardflags_lo = tuple->TupleData[1] |
  597. ((u16)tuple->TupleData[2] << 8);
  598. break;
  599. case SSB_PCMCIA_CIS_LEDS:
  600. GOTO_ERROR_ON(tuple->TupleDataLen != 5,
  601. "leds tpl size");
  602. sprom->gpio0 = tuple->TupleData[1];
  603. sprom->gpio1 = tuple->TupleData[2];
  604. sprom->gpio2 = tuple->TupleData[3];
  605. sprom->gpio3 = tuple->TupleData[4];
  606. break;
  607. }
  608. return -ENOSPC; /* continue with next entry */
  609. error:
  610. pr_err("PCMCIA: Failed to fetch device invariants: %s\n",
  611. error_description);
  612. return -ENODEV;
  613. }
  614. int ssb_pcmcia_get_invariants(struct ssb_bus *bus,
  615. struct ssb_init_invariants *iv)
  616. {
  617. struct ssb_sprom *sprom = &iv->sprom;
  618. int res;
  619. memset(sprom, 0xFF, sizeof(*sprom));
  620. sprom->revision = 1;
  621. sprom->boardflags_lo = 0;
  622. sprom->boardflags_hi = 0;
  623. /* First fetch the MAC address. */
  624. res = pcmcia_loop_tuple(bus->host_pcmcia, CISTPL_FUNCE,
  625. ssb_pcmcia_get_mac, sprom);
  626. if (res != 0) {
  627. pr_err("PCMCIA: Failed to fetch MAC address\n");
  628. return -ENODEV;
  629. }
  630. /* Fetch the vendor specific tuples. */
  631. res = pcmcia_loop_tuple(bus->host_pcmcia, SSB_PCMCIA_CIS,
  632. ssb_pcmcia_do_get_invariants, iv);
  633. if ((res == 0) || (res == -ENOSPC))
  634. return 0;
  635. pr_err("PCMCIA: Failed to fetch device invariants\n");
  636. return -ENODEV;
  637. }
  638. static ssize_t ssb_pcmcia_attr_sprom_show(struct device *pcmciadev,
  639. struct device_attribute *attr,
  640. char *buf)
  641. {
  642. struct pcmcia_device *pdev =
  643. container_of(pcmciadev, struct pcmcia_device, dev);
  644. struct ssb_bus *bus;
  645. bus = ssb_pcmcia_dev_to_bus(pdev);
  646. if (!bus)
  647. return -ENODEV;
  648. return ssb_attr_sprom_show(bus, buf,
  649. ssb_pcmcia_sprom_read_all);
  650. }
  651. static ssize_t ssb_pcmcia_attr_sprom_store(struct device *pcmciadev,
  652. struct device_attribute *attr,
  653. const char *buf, size_t count)
  654. {
  655. struct pcmcia_device *pdev =
  656. container_of(pcmciadev, struct pcmcia_device, dev);
  657. struct ssb_bus *bus;
  658. bus = ssb_pcmcia_dev_to_bus(pdev);
  659. if (!bus)
  660. return -ENODEV;
  661. return ssb_attr_sprom_store(bus, buf, count,
  662. ssb_pcmcia_sprom_check_crc,
  663. ssb_pcmcia_sprom_write_all);
  664. }
  665. static DEVICE_ATTR(ssb_sprom, 0600,
  666. ssb_pcmcia_attr_sprom_show,
  667. ssb_pcmcia_attr_sprom_store);
  668. static int ssb_pcmcia_cor_setup(struct ssb_bus *bus, u8 cor)
  669. {
  670. u8 val;
  671. int err;
  672. err = ssb_pcmcia_cfg_read(bus, cor, &val);
  673. if (err)
  674. return err;
  675. val &= ~COR_SOFT_RESET;
  676. val |= COR_FUNC_ENA | COR_IREQ_ENA | COR_LEVEL_REQ;
  677. err = ssb_pcmcia_cfg_write(bus, cor, val);
  678. if (err)
  679. return err;
  680. msleep(40);
  681. return 0;
  682. }
  683. /* Initialize the PCMCIA hardware. This is called on Init and Resume. */
  684. int ssb_pcmcia_hardware_setup(struct ssb_bus *bus)
  685. {
  686. int err;
  687. if (bus->bustype != SSB_BUSTYPE_PCMCIA)
  688. return 0;
  689. /* Switch segment to a known state and sync
  690. * bus->mapped_pcmcia_seg with hardware state. */
  691. ssb_pcmcia_switch_segment(bus, 0);
  692. /* Init the COR register. */
  693. err = ssb_pcmcia_cor_setup(bus, CISREG_COR);
  694. if (err)
  695. return err;
  696. /* Some cards also need this register to get poked. */
  697. err = ssb_pcmcia_cor_setup(bus, CISREG_COR + 0x80);
  698. if (err)
  699. return err;
  700. return 0;
  701. }
  702. void ssb_pcmcia_exit(struct ssb_bus *bus)
  703. {
  704. if (bus->bustype != SSB_BUSTYPE_PCMCIA)
  705. return;
  706. device_remove_file(&bus->host_pcmcia->dev, &dev_attr_ssb_sprom);
  707. }
  708. int ssb_pcmcia_init(struct ssb_bus *bus)
  709. {
  710. int err;
  711. if (bus->bustype != SSB_BUSTYPE_PCMCIA)
  712. return 0;
  713. err = ssb_pcmcia_hardware_setup(bus);
  714. if (err)
  715. goto error;
  716. bus->sprom_size = SSB_PCMCIA_SPROM_SIZE;
  717. mutex_init(&bus->sprom_mutex);
  718. err = device_create_file(&bus->host_pcmcia->dev, &dev_attr_ssb_sprom);
  719. if (err)
  720. goto error;
  721. return 0;
  722. error:
  723. pr_err("Failed to initialize PCMCIA host device\n");
  724. return err;
  725. }