pch_phub.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/fs.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/string.h>
  23. #include <linux/pci.h>
  24. #include <linux/io.h>
  25. #include <linux/delay.h>
  26. #include <linux/mutex.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/ctype.h>
  29. #define PHUB_STATUS 0x00 /* Status Register offset */
  30. #define PHUB_CONTROL 0x04 /* Control Register offset */
  31. #define PHUB_TIMEOUT 0x05 /* Time out value for Status Register */
  32. #define PCH_PHUB_ROM_WRITE_ENABLE 0x01 /* Enabling for writing ROM */
  33. #define PCH_PHUB_ROM_WRITE_DISABLE 0x00 /* Disabling for writing ROM */
  34. #define PCH_PHUB_MAC_START_ADDR 0x20C /* MAC data area start address offset */
  35. #define PCH_PHUB_ROM_START_ADDR_EG20T 0x14 /* ROM data area start address offset
  36. (Intel EG20T PCH)*/
  37. #define PCH_PHUB_ROM_START_ADDR_ML7213 0x400 /* ROM data area start address
  38. offset(OKI SEMICONDUCTOR ML7213)
  39. */
  40. /* MAX number of INT_REDUCE_CONTROL registers */
  41. #define MAX_NUM_INT_REDUCE_CONTROL_REG 128
  42. #define PCI_DEVICE_ID_PCH1_PHUB 0x8801
  43. #define PCH_MINOR_NOS 1
  44. #define CLKCFG_CAN_50MHZ 0x12000000
  45. #define CLKCFG_CANCLK_MASK 0xFF000000
  46. /* Macros for ML7213 */
  47. #define PCI_VENDOR_ID_ROHM 0x10db
  48. #define PCI_DEVICE_ID_ROHM_ML7213_PHUB 0x801A
  49. /* SROM ACCESS Macro */
  50. #define PCH_WORD_ADDR_MASK (~((1 << 2) - 1))
  51. /* Registers address offset */
  52. #define PCH_PHUB_ID_REG 0x0000
  53. #define PCH_PHUB_QUEUE_PRI_VAL_REG 0x0004
  54. #define PCH_PHUB_RC_QUEUE_MAXSIZE_REG 0x0008
  55. #define PCH_PHUB_BRI_QUEUE_MAXSIZE_REG 0x000C
  56. #define PCH_PHUB_COMP_RESP_TIMEOUT_REG 0x0010
  57. #define PCH_PHUB_BUS_SLAVE_CONTROL_REG 0x0014
  58. #define PCH_PHUB_DEADLOCK_AVOID_TYPE_REG 0x0018
  59. #define PCH_PHUB_INTPIN_REG_WPERMIT_REG0 0x0020
  60. #define PCH_PHUB_INTPIN_REG_WPERMIT_REG1 0x0024
  61. #define PCH_PHUB_INTPIN_REG_WPERMIT_REG2 0x0028
  62. #define PCH_PHUB_INTPIN_REG_WPERMIT_REG3 0x002C
  63. #define PCH_PHUB_INT_REDUCE_CONTROL_REG_BASE 0x0040
  64. #define CLKCFG_REG_OFFSET 0x500
  65. #define PCH_PHUB_OROM_SIZE 15360
  66. /**
  67. * struct pch_phub_reg - PHUB register structure
  68. * @phub_id_reg: PHUB_ID register val
  69. * @q_pri_val_reg: QUEUE_PRI_VAL register val
  70. * @rc_q_maxsize_reg: RC_QUEUE_MAXSIZE register val
  71. * @bri_q_maxsize_reg: BRI_QUEUE_MAXSIZE register val
  72. * @comp_resp_timeout_reg: COMP_RESP_TIMEOUT register val
  73. * @bus_slave_control_reg: BUS_SLAVE_CONTROL_REG register val
  74. * @deadlock_avoid_type_reg: DEADLOCK_AVOID_TYPE register val
  75. * @intpin_reg_wpermit_reg0: INTPIN_REG_WPERMIT register 0 val
  76. * @intpin_reg_wpermit_reg1: INTPIN_REG_WPERMIT register 1 val
  77. * @intpin_reg_wpermit_reg2: INTPIN_REG_WPERMIT register 2 val
  78. * @intpin_reg_wpermit_reg3: INTPIN_REG_WPERMIT register 3 val
  79. * @int_reduce_control_reg: INT_REDUCE_CONTROL registers val
  80. * @clkcfg_reg: CLK CFG register val
  81. * @pch_phub_base_address: Register base address
  82. * @pch_phub_extrom_base_address: external rom base address
  83. */
  84. struct pch_phub_reg {
  85. u32 phub_id_reg;
  86. u32 q_pri_val_reg;
  87. u32 rc_q_maxsize_reg;
  88. u32 bri_q_maxsize_reg;
  89. u32 comp_resp_timeout_reg;
  90. u32 bus_slave_control_reg;
  91. u32 deadlock_avoid_type_reg;
  92. u32 intpin_reg_wpermit_reg0;
  93. u32 intpin_reg_wpermit_reg1;
  94. u32 intpin_reg_wpermit_reg2;
  95. u32 intpin_reg_wpermit_reg3;
  96. u32 int_reduce_control_reg[MAX_NUM_INT_REDUCE_CONTROL_REG];
  97. u32 clkcfg_reg;
  98. void __iomem *pch_phub_base_address;
  99. void __iomem *pch_phub_extrom_base_address;
  100. };
  101. /* SROM SPEC for MAC address assignment offset */
  102. static const int pch_phub_mac_offset[ETH_ALEN] = {0x3, 0x2, 0x1, 0x0, 0xb, 0xa};
  103. static DEFINE_MUTEX(pch_phub_mutex);
  104. /**
  105. * pch_phub_read_modify_write_reg() - Reading modifying and writing register
  106. * @reg_addr_offset: Register offset address value.
  107. * @data: Writing value.
  108. * @mask: Mask value.
  109. */
  110. static void pch_phub_read_modify_write_reg(struct pch_phub_reg *chip,
  111. unsigned int reg_addr_offset,
  112. unsigned int data, unsigned int mask)
  113. {
  114. void __iomem *reg_addr = chip->pch_phub_base_address + reg_addr_offset;
  115. iowrite32(((ioread32(reg_addr) & ~mask)) | data, reg_addr);
  116. }
  117. /* pch_phub_save_reg_conf - saves register configuration */
  118. static void pch_phub_save_reg_conf(struct pci_dev *pdev)
  119. {
  120. unsigned int i;
  121. struct pch_phub_reg *chip = pci_get_drvdata(pdev);
  122. void __iomem *p = chip->pch_phub_base_address;
  123. chip->phub_id_reg = ioread32(p + PCH_PHUB_ID_REG);
  124. chip->q_pri_val_reg = ioread32(p + PCH_PHUB_QUEUE_PRI_VAL_REG);
  125. chip->rc_q_maxsize_reg = ioread32(p + PCH_PHUB_RC_QUEUE_MAXSIZE_REG);
  126. chip->bri_q_maxsize_reg = ioread32(p + PCH_PHUB_BRI_QUEUE_MAXSIZE_REG);
  127. chip->comp_resp_timeout_reg =
  128. ioread32(p + PCH_PHUB_COMP_RESP_TIMEOUT_REG);
  129. chip->bus_slave_control_reg =
  130. ioread32(p + PCH_PHUB_BUS_SLAVE_CONTROL_REG);
  131. chip->deadlock_avoid_type_reg =
  132. ioread32(p + PCH_PHUB_DEADLOCK_AVOID_TYPE_REG);
  133. chip->intpin_reg_wpermit_reg0 =
  134. ioread32(p + PCH_PHUB_INTPIN_REG_WPERMIT_REG0);
  135. chip->intpin_reg_wpermit_reg1 =
  136. ioread32(p + PCH_PHUB_INTPIN_REG_WPERMIT_REG1);
  137. chip->intpin_reg_wpermit_reg2 =
  138. ioread32(p + PCH_PHUB_INTPIN_REG_WPERMIT_REG2);
  139. chip->intpin_reg_wpermit_reg3 =
  140. ioread32(p + PCH_PHUB_INTPIN_REG_WPERMIT_REG3);
  141. dev_dbg(&pdev->dev, "%s : "
  142. "chip->phub_id_reg=%x, "
  143. "chip->q_pri_val_reg=%x, "
  144. "chip->rc_q_maxsize_reg=%x, "
  145. "chip->bri_q_maxsize_reg=%x, "
  146. "chip->comp_resp_timeout_reg=%x, "
  147. "chip->bus_slave_control_reg=%x, "
  148. "chip->deadlock_avoid_type_reg=%x, "
  149. "chip->intpin_reg_wpermit_reg0=%x, "
  150. "chip->intpin_reg_wpermit_reg1=%x, "
  151. "chip->intpin_reg_wpermit_reg2=%x, "
  152. "chip->intpin_reg_wpermit_reg3=%x\n", __func__,
  153. chip->phub_id_reg,
  154. chip->q_pri_val_reg,
  155. chip->rc_q_maxsize_reg,
  156. chip->bri_q_maxsize_reg,
  157. chip->comp_resp_timeout_reg,
  158. chip->bus_slave_control_reg,
  159. chip->deadlock_avoid_type_reg,
  160. chip->intpin_reg_wpermit_reg0,
  161. chip->intpin_reg_wpermit_reg1,
  162. chip->intpin_reg_wpermit_reg2,
  163. chip->intpin_reg_wpermit_reg3);
  164. for (i = 0; i < MAX_NUM_INT_REDUCE_CONTROL_REG; i++) {
  165. chip->int_reduce_control_reg[i] =
  166. ioread32(p + PCH_PHUB_INT_REDUCE_CONTROL_REG_BASE + 4 * i);
  167. dev_dbg(&pdev->dev, "%s : "
  168. "chip->int_reduce_control_reg[%d]=%x\n",
  169. __func__, i, chip->int_reduce_control_reg[i]);
  170. }
  171. chip->clkcfg_reg = ioread32(p + CLKCFG_REG_OFFSET);
  172. }
  173. /* pch_phub_restore_reg_conf - restore register configuration */
  174. static void pch_phub_restore_reg_conf(struct pci_dev *pdev)
  175. {
  176. unsigned int i;
  177. struct pch_phub_reg *chip = pci_get_drvdata(pdev);
  178. void __iomem *p;
  179. p = chip->pch_phub_base_address;
  180. iowrite32(chip->phub_id_reg, p + PCH_PHUB_ID_REG);
  181. iowrite32(chip->q_pri_val_reg, p + PCH_PHUB_QUEUE_PRI_VAL_REG);
  182. iowrite32(chip->rc_q_maxsize_reg, p + PCH_PHUB_RC_QUEUE_MAXSIZE_REG);
  183. iowrite32(chip->bri_q_maxsize_reg, p + PCH_PHUB_BRI_QUEUE_MAXSIZE_REG);
  184. iowrite32(chip->comp_resp_timeout_reg,
  185. p + PCH_PHUB_COMP_RESP_TIMEOUT_REG);
  186. iowrite32(chip->bus_slave_control_reg,
  187. p + PCH_PHUB_BUS_SLAVE_CONTROL_REG);
  188. iowrite32(chip->deadlock_avoid_type_reg,
  189. p + PCH_PHUB_DEADLOCK_AVOID_TYPE_REG);
  190. iowrite32(chip->intpin_reg_wpermit_reg0,
  191. p + PCH_PHUB_INTPIN_REG_WPERMIT_REG0);
  192. iowrite32(chip->intpin_reg_wpermit_reg1,
  193. p + PCH_PHUB_INTPIN_REG_WPERMIT_REG1);
  194. iowrite32(chip->intpin_reg_wpermit_reg2,
  195. p + PCH_PHUB_INTPIN_REG_WPERMIT_REG2);
  196. iowrite32(chip->intpin_reg_wpermit_reg3,
  197. p + PCH_PHUB_INTPIN_REG_WPERMIT_REG3);
  198. dev_dbg(&pdev->dev, "%s : "
  199. "chip->phub_id_reg=%x, "
  200. "chip->q_pri_val_reg=%x, "
  201. "chip->rc_q_maxsize_reg=%x, "
  202. "chip->bri_q_maxsize_reg=%x, "
  203. "chip->comp_resp_timeout_reg=%x, "
  204. "chip->bus_slave_control_reg=%x, "
  205. "chip->deadlock_avoid_type_reg=%x, "
  206. "chip->intpin_reg_wpermit_reg0=%x, "
  207. "chip->intpin_reg_wpermit_reg1=%x, "
  208. "chip->intpin_reg_wpermit_reg2=%x, "
  209. "chip->intpin_reg_wpermit_reg3=%x\n", __func__,
  210. chip->phub_id_reg,
  211. chip->q_pri_val_reg,
  212. chip->rc_q_maxsize_reg,
  213. chip->bri_q_maxsize_reg,
  214. chip->comp_resp_timeout_reg,
  215. chip->bus_slave_control_reg,
  216. chip->deadlock_avoid_type_reg,
  217. chip->intpin_reg_wpermit_reg0,
  218. chip->intpin_reg_wpermit_reg1,
  219. chip->intpin_reg_wpermit_reg2,
  220. chip->intpin_reg_wpermit_reg3);
  221. for (i = 0; i < MAX_NUM_INT_REDUCE_CONTROL_REG; i++) {
  222. iowrite32(chip->int_reduce_control_reg[i],
  223. p + PCH_PHUB_INT_REDUCE_CONTROL_REG_BASE + 4 * i);
  224. dev_dbg(&pdev->dev, "%s : "
  225. "chip->int_reduce_control_reg[%d]=%x\n",
  226. __func__, i, chip->int_reduce_control_reg[i]);
  227. }
  228. iowrite32(chip->clkcfg_reg, p + CLKCFG_REG_OFFSET);
  229. }
  230. /**
  231. * pch_phub_read_serial_rom() - Reading Serial ROM
  232. * @offset_address: Serial ROM offset address to read.
  233. * @data: Read buffer for specified Serial ROM value.
  234. */
  235. static void pch_phub_read_serial_rom(struct pch_phub_reg *chip,
  236. unsigned int offset_address, u8 *data)
  237. {
  238. void __iomem *mem_addr = chip->pch_phub_extrom_base_address +
  239. offset_address;
  240. *data = ioread8(mem_addr);
  241. }
  242. /**
  243. * pch_phub_write_serial_rom() - Writing Serial ROM
  244. * @offset_address: Serial ROM offset address.
  245. * @data: Serial ROM value to write.
  246. */
  247. static int pch_phub_write_serial_rom(struct pch_phub_reg *chip,
  248. unsigned int offset_address, u8 data)
  249. {
  250. void __iomem *mem_addr = chip->pch_phub_extrom_base_address +
  251. (offset_address & PCH_WORD_ADDR_MASK);
  252. int i;
  253. unsigned int word_data;
  254. unsigned int pos;
  255. unsigned int mask;
  256. pos = (offset_address % 4) * 8;
  257. mask = ~(0xFF << pos);
  258. iowrite32(PCH_PHUB_ROM_WRITE_ENABLE,
  259. chip->pch_phub_extrom_base_address + PHUB_CONTROL);
  260. word_data = ioread32(mem_addr);
  261. iowrite32((word_data & mask) | (u32)data << pos, mem_addr);
  262. i = 0;
  263. while (ioread8(chip->pch_phub_extrom_base_address +
  264. PHUB_STATUS) != 0x00) {
  265. msleep(1);
  266. if (i == PHUB_TIMEOUT)
  267. return -ETIMEDOUT;
  268. i++;
  269. }
  270. iowrite32(PCH_PHUB_ROM_WRITE_DISABLE,
  271. chip->pch_phub_extrom_base_address + PHUB_CONTROL);
  272. return 0;
  273. }
  274. /**
  275. * pch_phub_read_serial_rom_val() - Read Serial ROM value
  276. * @offset_address: Serial ROM address offset value.
  277. * @data: Serial ROM value to read.
  278. */
  279. static void pch_phub_read_serial_rom_val(struct pch_phub_reg *chip,
  280. unsigned int offset_address, u8 *data)
  281. {
  282. unsigned int mem_addr;
  283. mem_addr = PCH_PHUB_ROM_START_ADDR_EG20T +
  284. pch_phub_mac_offset[offset_address];
  285. pch_phub_read_serial_rom(chip, mem_addr, data);
  286. }
  287. /**
  288. * pch_phub_write_serial_rom_val() - writing Serial ROM value
  289. * @offset_address: Serial ROM address offset value.
  290. * @data: Serial ROM value.
  291. */
  292. static int pch_phub_write_serial_rom_val(struct pch_phub_reg *chip,
  293. unsigned int offset_address, u8 data)
  294. {
  295. int retval;
  296. unsigned int mem_addr;
  297. mem_addr = PCH_PHUB_ROM_START_ADDR_EG20T +
  298. pch_phub_mac_offset[offset_address];
  299. retval = pch_phub_write_serial_rom(chip, mem_addr, data);
  300. return retval;
  301. }
  302. /* pch_phub_gbe_serial_rom_conf - makes Serial ROM header format configuration
  303. * for Gigabit Ethernet MAC address
  304. */
  305. static int pch_phub_gbe_serial_rom_conf(struct pch_phub_reg *chip)
  306. {
  307. int retval;
  308. retval = pch_phub_write_serial_rom(chip, 0x0b, 0xbc);
  309. retval |= pch_phub_write_serial_rom(chip, 0x0a, 0x10);
  310. retval |= pch_phub_write_serial_rom(chip, 0x09, 0x01);
  311. retval |= pch_phub_write_serial_rom(chip, 0x08, 0x02);
  312. retval |= pch_phub_write_serial_rom(chip, 0x0f, 0x00);
  313. retval |= pch_phub_write_serial_rom(chip, 0x0e, 0x00);
  314. retval |= pch_phub_write_serial_rom(chip, 0x0d, 0x00);
  315. retval |= pch_phub_write_serial_rom(chip, 0x0c, 0x80);
  316. retval |= pch_phub_write_serial_rom(chip, 0x13, 0xbc);
  317. retval |= pch_phub_write_serial_rom(chip, 0x12, 0x10);
  318. retval |= pch_phub_write_serial_rom(chip, 0x11, 0x01);
  319. retval |= pch_phub_write_serial_rom(chip, 0x10, 0x18);
  320. retval |= pch_phub_write_serial_rom(chip, 0x1b, 0xbc);
  321. retval |= pch_phub_write_serial_rom(chip, 0x1a, 0x10);
  322. retval |= pch_phub_write_serial_rom(chip, 0x19, 0x01);
  323. retval |= pch_phub_write_serial_rom(chip, 0x18, 0x19);
  324. retval |= pch_phub_write_serial_rom(chip, 0x23, 0xbc);
  325. retval |= pch_phub_write_serial_rom(chip, 0x22, 0x10);
  326. retval |= pch_phub_write_serial_rom(chip, 0x21, 0x01);
  327. retval |= pch_phub_write_serial_rom(chip, 0x20, 0x3a);
  328. retval |= pch_phub_write_serial_rom(chip, 0x27, 0x01);
  329. retval |= pch_phub_write_serial_rom(chip, 0x26, 0x00);
  330. retval |= pch_phub_write_serial_rom(chip, 0x25, 0x00);
  331. retval |= pch_phub_write_serial_rom(chip, 0x24, 0x00);
  332. return retval;
  333. }
  334. /**
  335. * pch_phub_read_gbe_mac_addr() - Read Gigabit Ethernet MAC address
  336. * @offset_address: Gigabit Ethernet MAC address offset value.
  337. * @data: Buffer of the Gigabit Ethernet MAC address value.
  338. */
  339. static void pch_phub_read_gbe_mac_addr(struct pch_phub_reg *chip, u8 *data)
  340. {
  341. int i;
  342. for (i = 0; i < ETH_ALEN; i++)
  343. pch_phub_read_serial_rom_val(chip, i, &data[i]);
  344. }
  345. /**
  346. * pch_phub_write_gbe_mac_addr() - Write MAC address
  347. * @offset_address: Gigabit Ethernet MAC address offset value.
  348. * @data: Gigabit Ethernet MAC address value.
  349. */
  350. static int pch_phub_write_gbe_mac_addr(struct pch_phub_reg *chip, u8 *data)
  351. {
  352. int retval;
  353. int i;
  354. retval = pch_phub_gbe_serial_rom_conf(chip);
  355. if (retval)
  356. return retval;
  357. for (i = 0; i < ETH_ALEN; i++) {
  358. retval = pch_phub_write_serial_rom_val(chip, i, data[i]);
  359. if (retval)
  360. return retval;
  361. }
  362. return retval;
  363. }
  364. static ssize_t pch_phub_bin_read(struct file *filp, struct kobject *kobj,
  365. struct bin_attribute *attr, char *buf,
  366. loff_t off, size_t count)
  367. {
  368. unsigned int rom_signature;
  369. unsigned char rom_length;
  370. unsigned int tmp;
  371. unsigned int addr_offset;
  372. unsigned int orom_size;
  373. int ret;
  374. int err;
  375. struct pch_phub_reg *chip =
  376. dev_get_drvdata(container_of(kobj, struct device, kobj));
  377. ret = mutex_lock_interruptible(&pch_phub_mutex);
  378. if (ret) {
  379. err = -ERESTARTSYS;
  380. goto return_err_nomutex;
  381. }
  382. /* Get Rom signature */
  383. pch_phub_read_serial_rom(chip, 0x80, (unsigned char *)&rom_signature);
  384. rom_signature &= 0xff;
  385. pch_phub_read_serial_rom(chip, 0x81, (unsigned char *)&tmp);
  386. rom_signature |= (tmp & 0xff) << 8;
  387. if (rom_signature == 0xAA55) {
  388. pch_phub_read_serial_rom(chip, 0x82, &rom_length);
  389. orom_size = rom_length * 512;
  390. if (orom_size < off) {
  391. addr_offset = 0;
  392. goto return_ok;
  393. }
  394. if (orom_size < count) {
  395. addr_offset = 0;
  396. goto return_ok;
  397. }
  398. for (addr_offset = 0; addr_offset < count; addr_offset++) {
  399. pch_phub_read_serial_rom(chip, 0x80 + addr_offset + off,
  400. &buf[addr_offset]);
  401. }
  402. } else {
  403. err = -ENODATA;
  404. goto return_err;
  405. }
  406. return_ok:
  407. mutex_unlock(&pch_phub_mutex);
  408. return addr_offset;
  409. return_err:
  410. mutex_unlock(&pch_phub_mutex);
  411. return_err_nomutex:
  412. return err;
  413. }
  414. static ssize_t pch_phub_bin_write(struct file *filp, struct kobject *kobj,
  415. struct bin_attribute *attr,
  416. char *buf, loff_t off, size_t count)
  417. {
  418. int err;
  419. unsigned int addr_offset;
  420. int ret;
  421. struct pch_phub_reg *chip =
  422. dev_get_drvdata(container_of(kobj, struct device, kobj));
  423. ret = mutex_lock_interruptible(&pch_phub_mutex);
  424. if (ret)
  425. return -ERESTARTSYS;
  426. if (off > PCH_PHUB_OROM_SIZE) {
  427. addr_offset = 0;
  428. goto return_ok;
  429. }
  430. if (count > PCH_PHUB_OROM_SIZE) {
  431. addr_offset = 0;
  432. goto return_ok;
  433. }
  434. for (addr_offset = 0; addr_offset < count; addr_offset++) {
  435. if (PCH_PHUB_OROM_SIZE < off + addr_offset)
  436. goto return_ok;
  437. ret = pch_phub_write_serial_rom(chip, 0x80 + addr_offset + off,
  438. buf[addr_offset]);
  439. if (ret) {
  440. err = ret;
  441. goto return_err;
  442. }
  443. }
  444. return_ok:
  445. mutex_unlock(&pch_phub_mutex);
  446. return addr_offset;
  447. return_err:
  448. mutex_unlock(&pch_phub_mutex);
  449. return err;
  450. }
  451. static ssize_t show_pch_mac(struct device *dev, struct device_attribute *attr,
  452. char *buf)
  453. {
  454. u8 mac[8];
  455. struct pch_phub_reg *chip = dev_get_drvdata(dev);
  456. pch_phub_read_gbe_mac_addr(chip, mac);
  457. return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  458. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  459. }
  460. static ssize_t store_pch_mac(struct device *dev, struct device_attribute *attr,
  461. const char *buf, size_t count)
  462. {
  463. u8 mac[6];
  464. struct pch_phub_reg *chip = dev_get_drvdata(dev);
  465. if (count != 18)
  466. return -EINVAL;
  467. sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  468. (u32 *)&mac[0], (u32 *)&mac[1], (u32 *)&mac[2], (u32 *)&mac[3],
  469. (u32 *)&mac[4], (u32 *)&mac[5]);
  470. pch_phub_write_gbe_mac_addr(chip, mac);
  471. return count;
  472. }
  473. static DEVICE_ATTR(pch_mac, S_IRUGO | S_IWUSR, show_pch_mac, store_pch_mac);
  474. static struct bin_attribute pch_bin_attr = {
  475. .attr = {
  476. .name = "pch_firmware",
  477. .mode = S_IRUGO | S_IWUSR,
  478. },
  479. .size = PCH_PHUB_OROM_SIZE + 1,
  480. .read = pch_phub_bin_read,
  481. .write = pch_phub_bin_write,
  482. };
  483. static int __devinit pch_phub_probe(struct pci_dev *pdev,
  484. const struct pci_device_id *id)
  485. {
  486. int retval;
  487. int ret;
  488. ssize_t rom_size;
  489. struct pch_phub_reg *chip;
  490. chip = kzalloc(sizeof(struct pch_phub_reg), GFP_KERNEL);
  491. if (chip == NULL)
  492. return -ENOMEM;
  493. ret = pci_enable_device(pdev);
  494. if (ret) {
  495. dev_err(&pdev->dev,
  496. "%s : pci_enable_device FAILED(ret=%d)", __func__, ret);
  497. goto err_pci_enable_dev;
  498. }
  499. dev_dbg(&pdev->dev, "%s : pci_enable_device returns %d\n", __func__,
  500. ret);
  501. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  502. if (ret) {
  503. dev_err(&pdev->dev,
  504. "%s : pci_request_regions FAILED(ret=%d)", __func__, ret);
  505. goto err_req_regions;
  506. }
  507. dev_dbg(&pdev->dev, "%s : "
  508. "pci_request_regions returns %d\n", __func__, ret);
  509. chip->pch_phub_base_address = pci_iomap(pdev, 1, 0);
  510. if (chip->pch_phub_base_address == 0) {
  511. dev_err(&pdev->dev, "%s : pci_iomap FAILED", __func__);
  512. ret = -ENOMEM;
  513. goto err_pci_iomap;
  514. }
  515. dev_dbg(&pdev->dev, "%s : pci_iomap SUCCESS and value "
  516. "in pch_phub_base_address variable is %p\n", __func__,
  517. chip->pch_phub_base_address);
  518. chip->pch_phub_extrom_base_address = pci_map_rom(pdev, &rom_size);
  519. if (chip->pch_phub_extrom_base_address == 0) {
  520. dev_err(&pdev->dev, "%s : pci_map_rom FAILED", __func__);
  521. ret = -ENOMEM;
  522. goto err_pci_map;
  523. }
  524. dev_dbg(&pdev->dev, "%s : "
  525. "pci_map_rom SUCCESS and value in "
  526. "pch_phub_extrom_base_address variable is %p\n", __func__,
  527. chip->pch_phub_extrom_base_address);
  528. if (id->driver_data == 1) {
  529. retval = sysfs_create_file(&pdev->dev.kobj,
  530. &dev_attr_pch_mac.attr);
  531. if (retval)
  532. goto err_sysfs_create;
  533. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pch_bin_attr);
  534. if (retval)
  535. goto exit_bin_attr;
  536. pch_phub_read_modify_write_reg(chip,
  537. (unsigned int)CLKCFG_REG_OFFSET,
  538. CLKCFG_CAN_50MHZ,
  539. CLKCFG_CANCLK_MASK);
  540. /* set the prefech value */
  541. iowrite32(0x000affaa, chip->pch_phub_base_address + 0x14);
  542. /* set the interrupt delay value */
  543. iowrite32(0x25, chip->pch_phub_base_address + 0x44);
  544. } else if (id->driver_data == 2) {
  545. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pch_bin_attr);
  546. if (retval)
  547. goto err_sysfs_create;
  548. /* set the prefech value
  549. * Device2(USB OHCI #1/ USB EHCI #1/ USB Device):a
  550. * Device4(SDIO #0,1,2):f
  551. * Device6(SATA 2):f
  552. * Device8(USB OHCI #0/ USB EHCI #0):a
  553. */
  554. iowrite32(0x000affa0, chip->pch_phub_base_address + 0x14);
  555. }
  556. pci_set_drvdata(pdev, chip);
  557. return 0;
  558. exit_bin_attr:
  559. sysfs_remove_file(&pdev->dev.kobj, &dev_attr_pch_mac.attr);
  560. err_sysfs_create:
  561. pci_unmap_rom(pdev, chip->pch_phub_extrom_base_address);
  562. err_pci_map:
  563. pci_iounmap(pdev, chip->pch_phub_base_address);
  564. err_pci_iomap:
  565. pci_release_regions(pdev);
  566. err_req_regions:
  567. pci_disable_device(pdev);
  568. err_pci_enable_dev:
  569. kfree(chip);
  570. dev_err(&pdev->dev, "%s returns %d\n", __func__, ret);
  571. return ret;
  572. }
  573. static void __devexit pch_phub_remove(struct pci_dev *pdev)
  574. {
  575. struct pch_phub_reg *chip = pci_get_drvdata(pdev);
  576. sysfs_remove_file(&pdev->dev.kobj, &dev_attr_pch_mac.attr);
  577. sysfs_remove_bin_file(&pdev->dev.kobj, &pch_bin_attr);
  578. pci_unmap_rom(pdev, chip->pch_phub_extrom_base_address);
  579. pci_iounmap(pdev, chip->pch_phub_base_address);
  580. pci_release_regions(pdev);
  581. pci_disable_device(pdev);
  582. kfree(chip);
  583. }
  584. #ifdef CONFIG_PM
  585. static int pch_phub_suspend(struct pci_dev *pdev, pm_message_t state)
  586. {
  587. int ret;
  588. pch_phub_save_reg_conf(pdev);
  589. ret = pci_save_state(pdev);
  590. if (ret) {
  591. dev_err(&pdev->dev,
  592. " %s -pci_save_state returns %d\n", __func__, ret);
  593. return ret;
  594. }
  595. pci_enable_wake(pdev, PCI_D3hot, 0);
  596. pci_disable_device(pdev);
  597. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  598. return 0;
  599. }
  600. static int pch_phub_resume(struct pci_dev *pdev)
  601. {
  602. int ret;
  603. pci_set_power_state(pdev, PCI_D0);
  604. pci_restore_state(pdev);
  605. ret = pci_enable_device(pdev);
  606. if (ret) {
  607. dev_err(&pdev->dev,
  608. "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
  609. return ret;
  610. }
  611. pci_enable_wake(pdev, PCI_D3hot, 0);
  612. pch_phub_restore_reg_conf(pdev);
  613. return 0;
  614. }
  615. #else
  616. #define pch_phub_suspend NULL
  617. #define pch_phub_resume NULL
  618. #endif /* CONFIG_PM */
  619. static struct pci_device_id pch_phub_pcidev_id[] = {
  620. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_PCH1_PHUB), 1, },
  621. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ROHM_ML7213_PHUB), 2, },
  622. { }
  623. };
  624. static struct pci_driver pch_phub_driver = {
  625. .name = "pch_phub",
  626. .id_table = pch_phub_pcidev_id,
  627. .probe = pch_phub_probe,
  628. .remove = __devexit_p(pch_phub_remove),
  629. .suspend = pch_phub_suspend,
  630. .resume = pch_phub_resume
  631. };
  632. static int __init pch_phub_pci_init(void)
  633. {
  634. return pci_register_driver(&pch_phub_driver);
  635. }
  636. static void __exit pch_phub_pci_exit(void)
  637. {
  638. pci_unregister_driver(&pch_phub_driver);
  639. }
  640. module_init(pch_phub_pci_init);
  641. module_exit(pch_phub_pci_exit);
  642. MODULE_DESCRIPTION("PCH Packet Hub PCI Driver");
  643. MODULE_LICENSE("GPL");