setup-cardbus.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cardbus bridge setup routines.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/errno.h>
  7. #include <linux/ioport.h>
  8. #include <linux/pci.h>
  9. #include <linux/sizes.h>
  10. #include <linux/sprintf.h>
  11. #include <linux/types.h>
  12. #include "pci.h"
  13. #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
  14. #define CARDBUS_RESERVE_BUSNR 3
  15. #define DEFAULT_CARDBUS_IO_SIZE SZ_256
  16. #define DEFAULT_CARDBUS_MEM_SIZE SZ_64M
  17. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  18. static unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  19. static unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  20. unsigned long pci_cardbus_resource_alignment(struct resource *res)
  21. {
  22. if (res->flags & IORESOURCE_IO)
  23. return pci_cardbus_io_size;
  24. if (res->flags & IORESOURCE_MEM)
  25. return pci_cardbus_mem_size;
  26. return 0;
  27. }
  28. int pci_bus_size_cardbus_bridge(struct pci_bus *bus,
  29. struct list_head *realloc_head)
  30. {
  31. struct pci_dev *bridge = bus->self;
  32. struct resource *b_res;
  33. resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
  34. u16 ctrl;
  35. b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW];
  36. if (resource_assigned(b_res))
  37. goto handle_b_res_1;
  38. /*
  39. * Reserve some resources for CardBus. We reserve a fixed amount
  40. * of bus space for CardBus bridges.
  41. */
  42. resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size);
  43. b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  44. if (realloc_head) {
  45. b_res->end -= pci_cardbus_io_size;
  46. pci_dev_res_add_to_list(realloc_head, bridge, b_res,
  47. pci_cardbus_io_size,
  48. pci_cardbus_io_size);
  49. }
  50. handle_b_res_1:
  51. b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW];
  52. if (resource_assigned(b_res))
  53. goto handle_b_res_2;
  54. resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size);
  55. b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  56. if (realloc_head) {
  57. b_res->end -= pci_cardbus_io_size;
  58. pci_dev_res_add_to_list(realloc_head, bridge, b_res,
  59. pci_cardbus_io_size,
  60. pci_cardbus_io_size);
  61. }
  62. handle_b_res_2:
  63. /* MEM1 must not be pref MMIO */
  64. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  65. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
  66. ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
  67. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  68. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  69. }
  70. /* Check whether prefetchable memory is supported by this bridge. */
  71. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  72. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  73. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  74. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  75. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  76. }
  77. b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW];
  78. if (resource_assigned(b_res))
  79. goto handle_b_res_3;
  80. /*
  81. * If we have prefetchable memory support, allocate two regions.
  82. * Otherwise, allocate one region of twice the size.
  83. */
  84. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  85. resource_set_range(b_res, pci_cardbus_mem_size,
  86. pci_cardbus_mem_size);
  87. b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
  88. IORESOURCE_STARTALIGN;
  89. if (realloc_head) {
  90. b_res->end -= pci_cardbus_mem_size;
  91. pci_dev_res_add_to_list(realloc_head, bridge, b_res,
  92. pci_cardbus_mem_size,
  93. pci_cardbus_mem_size);
  94. }
  95. /* Reduce that to half */
  96. b_res_3_size = pci_cardbus_mem_size;
  97. }
  98. handle_b_res_3:
  99. b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW];
  100. if (resource_assigned(b_res))
  101. goto handle_done;
  102. resource_set_range(b_res, pci_cardbus_mem_size, b_res_3_size);
  103. b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
  104. if (realloc_head) {
  105. b_res->end -= b_res_3_size;
  106. pci_dev_res_add_to_list(realloc_head, bridge, b_res,
  107. b_res_3_size, pci_cardbus_mem_size);
  108. }
  109. handle_done:
  110. return 0;
  111. }
  112. void pci_setup_cardbus_bridge(struct pci_bus *bus)
  113. {
  114. struct pci_dev *bridge = bus->self;
  115. struct resource *res;
  116. struct pci_bus_region region;
  117. pci_info(bridge, "CardBus bridge to %pR\n",
  118. &bus->busn_res);
  119. res = bus->resource[0];
  120. pcibios_resource_to_bus(bridge->bus, &region, res);
  121. if (resource_assigned(res) && res->flags & IORESOURCE_IO) {
  122. /*
  123. * The IO resource is allocated a range twice as large as it
  124. * would normally need. This allows us to set both IO regs.
  125. */
  126. pci_info(bridge, " bridge window %pR\n", res);
  127. pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
  128. region.start);
  129. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
  130. region.end);
  131. }
  132. res = bus->resource[1];
  133. pcibios_resource_to_bus(bridge->bus, &region, res);
  134. if (resource_assigned(res) && res->flags & IORESOURCE_IO) {
  135. pci_info(bridge, " bridge window %pR\n", res);
  136. pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
  137. region.start);
  138. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
  139. region.end);
  140. }
  141. res = bus->resource[2];
  142. pcibios_resource_to_bus(bridge->bus, &region, res);
  143. if (resource_assigned(res) && res->flags & IORESOURCE_MEM) {
  144. pci_info(bridge, " bridge window %pR\n", res);
  145. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
  146. region.start);
  147. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
  148. region.end);
  149. }
  150. res = bus->resource[3];
  151. pcibios_resource_to_bus(bridge->bus, &region, res);
  152. if (resource_assigned(res) && res->flags & IORESOURCE_MEM) {
  153. pci_info(bridge, " bridge window %pR\n", res);
  154. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
  155. region.start);
  156. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
  157. region.end);
  158. }
  159. }
  160. EXPORT_SYMBOL(pci_setup_cardbus_bridge);
  161. int pci_setup_cardbus(char *str)
  162. {
  163. if (!strncmp(str, "cbiosize=", 9)) {
  164. pci_cardbus_io_size = memparse(str + 9, &str);
  165. return 0;
  166. } else if (!strncmp(str, "cbmemsize=", 10)) {
  167. pci_cardbus_mem_size = memparse(str + 10, &str);
  168. return 0;
  169. }
  170. return -ENOENT;
  171. }
  172. int pci_cardbus_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
  173. u32 buses, int max,
  174. unsigned int available_buses, int pass)
  175. {
  176. struct pci_bus *child;
  177. bool fixed_buses;
  178. u8 fixed_sec, fixed_sub;
  179. int next_busnr;
  180. u32 i, j = 0;
  181. /*
  182. * We need to assign a number to this bus which we always do in the
  183. * second pass.
  184. */
  185. if (!pass) {
  186. /*
  187. * Temporarily disable forwarding of the configuration
  188. * cycles on all bridges in this bus segment to avoid
  189. * possible conflicts in the second pass between two bridges
  190. * programmed with overlapping bus ranges.
  191. */
  192. pci_write_config_dword(dev, PCI_PRIMARY_BUS,
  193. buses & PCI_SEC_LATENCY_TIMER_MASK);
  194. return max;
  195. }
  196. /* Clear errors */
  197. pci_write_config_word(dev, PCI_STATUS, 0xffff);
  198. /* Read bus numbers from EA Capability (if present) */
  199. fixed_buses = pci_ea_fixed_busnrs(dev, &fixed_sec, &fixed_sub);
  200. if (fixed_buses)
  201. next_busnr = fixed_sec;
  202. else
  203. next_busnr = max + 1;
  204. /*
  205. * Prevent assigning a bus number that already exists. This can
  206. * happen when a bridge is hot-plugged, so in this case we only
  207. * re-scan this bus.
  208. */
  209. child = pci_find_bus(pci_domain_nr(bus), next_busnr);
  210. if (!child) {
  211. child = pci_add_new_bus(bus, dev, next_busnr);
  212. if (!child)
  213. return max;
  214. pci_bus_insert_busn_res(child, next_busnr, bus->busn_res.end);
  215. }
  216. max++;
  217. if (available_buses)
  218. available_buses--;
  219. buses = (buses & PCI_SEC_LATENCY_TIMER_MASK) |
  220. FIELD_PREP(PCI_PRIMARY_BUS_MASK, child->primary) |
  221. FIELD_PREP(PCI_SECONDARY_BUS_MASK, child->busn_res.start) |
  222. FIELD_PREP(PCI_SUBORDINATE_BUS_MASK, child->busn_res.end);
  223. /*
  224. * yenta.c forces a secondary latency timer of 176.
  225. * Copy that behaviour here.
  226. */
  227. buses &= ~PCI_SEC_LATENCY_TIMER_MASK;
  228. buses |= FIELD_PREP(PCI_SEC_LATENCY_TIMER_MASK, CARDBUS_LATENCY_TIMER);
  229. /* We need to blast all three values with a single write */
  230. pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
  231. /*
  232. * For CardBus bridges, we leave 4 bus numbers as cards with a
  233. * PCI-to-PCI bridge can be inserted later.
  234. */
  235. for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
  236. struct pci_bus *parent = bus;
  237. if (pci_find_bus(pci_domain_nr(bus), max + i + 1))
  238. break;
  239. while (parent->parent) {
  240. if (!pcibios_assign_all_busses() &&
  241. (parent->busn_res.end > max) &&
  242. (parent->busn_res.end <= max + i)) {
  243. j = 1;
  244. }
  245. parent = parent->parent;
  246. }
  247. if (j) {
  248. /*
  249. * Often, there are two CardBus bridges -- try to
  250. * leave one valid bus number for each one.
  251. */
  252. i /= 2;
  253. break;
  254. }
  255. }
  256. max += i;
  257. /*
  258. * Set subordinate bus number to its real value. If fixed
  259. * subordinate bus number exists from EA capability then use it.
  260. */
  261. if (fixed_buses)
  262. max = fixed_sub;
  263. pci_bus_update_busn_res_end(child, max);
  264. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
  265. scnprintf(child->name, sizeof(child->name), "PCI CardBus %04x:%02x",
  266. pci_domain_nr(bus), child->number);
  267. pbus_validate_busn(child);
  268. return max;
  269. }