Debian Codegraph @

himbaechel/uarch/gowin/pack.cc

raw
1 #include <map>
2
3 #include "design_utils.h"
4 #include "log.h"
5 #include "nextpnr.h"
6
7 #define HIMBAECHEL_CONSTIDS "uarch/gowin/constids.inc"
8 #include "himbaechel_constids.h"
9 #include "himbaechel_helpers.h"
10
11 #include "gowin.h"
12 #include "gowin_utils.h"
13 #include "pack.h"
14
15 #include <cinttypes>
16
17 NEXTPNR_NAMESPACE_BEGIN
18
19 // ===================================
20 // Global set/reset
21 // ===================================
22 void GowinPacker::pack_gsr(void)
23 {
24 log_info("Pack GSR...\n");
25
26 bool user_gsr = false;
27 for (auto &cell : ctx->cells) {
28 auto &ci = *cell.second;
29
30 if (ci.type == id_GSR) {
31 user_gsr = true;
32 break;
33 }
34 }
35 if (!user_gsr) {
36 // make default GSR
37 auto gsr_cell = std::make_unique<CellInfo>(ctx, id_GSR, id_GSR);
38 gsr_cell->addInput(id_GSRI);
39 gsr_cell->connectPort(id_GSRI, ctx->nets.at(ctx->id("$PACKER_VCC")).get());
40 ctx->cells[gsr_cell->name] = std::move(gsr_cell);
41 }
42 if (ctx->verbose) {
43 if (user_gsr) {
44 log_info("Have user GSR\n");
45 } else {
46 log_info("No user GSR. Make one.\n");
47 }
48 }
49 }
50
51 // ===================================
52 // Pin function configuration via wires
53 // ===================================
54 void GowinPacker::pack_pincfg(void)
55 {
56 if (!gwu.has_PINCFG()) {
57 return;
58 }
59 log_info("Pack PINCFG...\n");
60
61 auto pincfg_cell = std::make_unique<CellInfo>(ctx, id_PINCFG, id_PINCFG);
62
63 const int pin_cnt = gwu.has_I2CCFG() ? 5 : 4;
64 for (int i = 0; i < pin_cnt; ++i) {
65 IdString port = ctx->idf("UNK%d_VCC", i);
66 pincfg_cell->addInput(port);
67 if (i && gwu.need_CFGPINS_INVERSION()) {
68 pincfg_cell->connectPort(port, ctx->nets.at(ctx->id("$PACKER_GND")).get());
69 } else {
70 pincfg_cell->connectPort(port, ctx->nets.at(ctx->id("$PACKER_VCC")).get());
71 }
72 }
73
74 const ArchArgs &args = ctx->args;
75
76 pincfg_cell->addInput(id_SSPI);
77 if (args.options.count("sspi_as_gpio")) {
78 pincfg_cell->connectPort(id_SSPI, ctx->nets.at(ctx->id("$PACKER_VCC")).get());
79 pincfg_cell->setParam(id_SSPI, 1);
80 } else {
81 pincfg_cell->connectPort(id_SSPI, ctx->nets.at(ctx->id("$PACKER_GND")).get());
82 }
83
84 if (gwu.has_I2CCFG()) {
85 pincfg_cell->addInput(id_I2C);
86 if (args.options.count("i2c_as_gpio")) {
87 pincfg_cell->connectPort(id_I2C, ctx->nets.at(ctx->id("$PACKER_VCC")).get());
88 pincfg_cell->setParam(id_I2C, 1);
89 } else {
90 pincfg_cell->connectPort(id_I2C, ctx->nets.at(ctx->id("$PACKER_GND")).get());
91 }
92 }
93 ctx->cells[pincfg_cell->name] = std::move(pincfg_cell);
94 }
95
96 // ===================================
97 // Global power regulator
98 // ===================================
99 void GowinPacker::pack_bandgap(void)
100 {
101 if (!gwu.has_BANDGAP()) {
102 return;
103 }
104 log_info("Pack BANDGAP...\n");
105
106 bool user_bandgap = false;
107 for (auto &cell : ctx->cells) {
108 auto &ci = *cell.second;
109
110 if (ci.type == id_BANDGAP) {
111 user_bandgap = true;
112 break;
113 }
114 }
115 if (!user_bandgap) {
116 // make default BANDGAP
117 auto bandgap_cell = std::make_unique<CellInfo>(ctx, id_BANDGAP, id_BANDGAP);
118 bandgap_cell->addInput(id_BGEN);
119 bandgap_cell->connectPort(id_BGEN, ctx->nets.at(ctx->id("$PACKER_VCC")).get());
120 ctx->cells[bandgap_cell->name] = std::move(bandgap_cell);
121 }
122 if (ctx->verbose) {
123 if (user_bandgap) {
124 log_info("Have user BANDGAP\n");
125 } else {
126 log_info("No user BANDGAP. Make one.\n");
127 }
128 }
129 }
130
131 // ===================================
132 // Replace INV with LUT
133 // ===================================
134 void GowinPacker::pack_inv(void)
135 {
136 log_info("Pack INV...\n");
137
138 for (auto &cell : ctx->cells) {
139 auto &ci = *cell.second;
140
141 if (ci.type == id_INV) {
142 ci.type = id_LUT4;
143 ci.renamePort(id_O, id_F);
144 ci.renamePort(id_I, id_I3); // use D - it's simple for INIT
145 ci.params[id_INIT] = Property(0x00ff);
146 }
147 }
148 }
149
150 // ===================================
151 // PLL
152 // ===================================
153 void GowinPacker::pack_pll(void)
154 {
155 log_info("Pack PLL...\n");
156
157 pool<BelId> used_pll_bels;
158
159 for (auto &cell : ctx->cells) {
160 auto &ci = *cell.second;
161
162 if (ci.type.in(id_rPLL, id_PLLVR, id_PLLA)) {
163 gwu.remove_brackets(&ci);
164
165 // If CLKIN is connected to a special pin, then it makes sense
166 // to try to place the PLL so that it uses a direct connection
167 // to this pin.
168 if (ci.bel == BelId()) {
169 NetInfo *ni = ci.getPort(id_CLKIN);
170 if (ni && ni->driver.cell && ni->driver.cell->bel != BelId()) {
171 BelId pll_bel = gwu.get_pll_bel(ni->driver.cell->bel, id_CLKIN_T);
172 if (ctx->debug) {
173 log_info("PLL clkin driver:%s at %s, PLL bel:%s\n", ctx->nameOf(ni->driver.cell),
174 ctx->getBelName(ni->driver.cell->bel).str(ctx).c_str(),
175 pll_bel != BelId() ? ctx->getBelName(pll_bel).str(ctx).c_str() : "NULL");
176 }
177 if (pll_bel != BelId() && used_pll_bels.count(pll_bel) == 0) {
178 used_pll_bels.insert(pll_bel);
179 ctx->bindBel(pll_bel, &ci, PlaceStrength::STRENGTH_LOCKED);
180 ci.disconnectPort(id_CLKIN);
181 ci.setParam(id_INSEL, std::string("CLKIN0"));
182 }
183 }
184 }
185 }
186 }
187 }
188
189 // ===================================
190 // ADC
191 // ===================================
192 void GowinPacker::pack_adc(void)
193 {
194 log_info("Pack ADC...\n");
195
196 for (auto &cell : ctx->cells) {
197 auto &ci = *cell.second;
198
199 if (is_adc(&ci)) {
200 gwu.remove_brackets(&ci);
201 }
202 }
203 }
204
205 // ===================================
206 // HCLK -- CLKDIV and CLKDIV2 for now
207 // ===================================
208 void GowinPacker::pack_hclk(void)
209 {
210 log_info("Pack HCLK cells...\n");
211
212 for (auto &cell : ctx->cells) {
213 auto ci = cell.second.get();
214 if (ci->type != id_CLKDIV)
215 continue;
216 NetInfo *hclk_in = ci->getPort(id_HCLKIN);
217 if (hclk_in) {
218 CellInfo *this_driver = hclk_in->driver.cell;
219 if (this_driver && this_driver->type == id_CLKDIV2) {
220 NetInfo *out = this_driver->getPort(id_CLKOUT);
221 if (out->users.entries() > 1) {
222 // We could do as the IDE does sometimes and replicate the CLKDIV2 cell
223 // as many times as we need. For now, we keep things simple
224 log_error("CLKDIV2 that drives CLKDIV should drive no other cells\n");
225 }
226 ci->cluster = ci->name;
227 this_driver->cluster = ci->name;
228 ci->constr_children.push_back(this_driver);
229 this_driver->constr_x = 0;
230 this_driver->constr_y = 0;
231 this_driver->constr_z = BelZ::CLKDIV2_0_Z - BelZ::CLKDIV_0_Z;
232 this_driver->constr_abs_z = false;
233 }
234 }
235 }
236 }
237
238 // ===================================
239 // DLLDLY
240 // ===================================
241 void GowinPacker::pack_dlldly(void)
242 {
243 log_info("Pack DLLDLYs...\n");
244
245 for (auto &cell : ctx->cells) {
246 auto ci = cell.second.get();
247 if (ci->type != id_DLLDLY)
248 continue;
249 NetInfo *clkin_net = ci->getPort(id_CLKIN);
250 NetInfo *clkout_net = ci->getPort(id_CLKOUT);
251 if (clkin_net == nullptr || clkout_net == nullptr) {
252 log_error("%s cell has unconnected CLKIN or CLKOUT pins.\n", ctx->nameOf(ci));
253 }
254 CellInfo *clk_src = clkin_net->driver.cell;
255 if (!is_io(clk_src)) {
256 log_error("Clock source for DLLDLY %s is not IO: %s.\n", ctx->nameOf(ci), ctx->nameOf(clk_src));
257 }
258 // DLLDLY placement is fixed
259 BelId io_bel = clk_src->bel;
260 BelId dlldly_bel = gwu.get_dlldly_bel(io_bel);
261 if (dlldly_bel == BelId()) {
262 log_error("Can't use IO %s as input for DLLDLY %s.\n", ctx->nameOf(clk_src), ctx->nameOf(ci));
263 }
264 if (ctx->verbose) {
265 log_info(" pack %s to use clock pin at %s\n", ctx->nameOf(ci), ctx->nameOfBel(io_bel));
266 }
267 ctx->bindBel(dlldly_bel, ci, STRENGTH_LOCKED);
268 gwu.remove_brackets(ci);
269 }
270 }
271
272 // =========================================
273 // Create entry points to the clock system
274 // =========================================
275 void GowinPacker::pack_buffered_nets(void)
276 {
277 log_info("Pack buffered nets...\n");
278
279 for (auto &net : ctx->nets) {
280 NetInfo *ni = net.second.get();
281 if (ni->driver.cell == nullptr || ni->users.empty() || net.first == ctx->id("$PACKER_GND") ||
282 net.first == ctx->id("$PACKER_VCC")) {
283 continue;
284 }
285 if (ni->attrs.count(id_CLOCK) == 0) {
286 if (ctx->settings.count(id_NO_GP_CLOCK_ROUTING)) {
287 continue;
288 }
289 if (gwu.driver_is_clksrc(ni->driver) || (!gwu.driver_is_io(ni->driver))) {
290 // no need for buffering
291 continue;
292 }
293 // check users for the clock inputs
294 bool has_clock_users = false;
295 for (auto usr : ni->users) {
296 if (usr.port.in(id_CLKIN, id_CLK, id_CLK0, id_CLK1, id_CLK2, id_CLK3, id_CLKFB)) {
297 has_clock_users = true;
298 break;
299 }
300 }
301 if (!has_clock_users) {
302 continue;
303 }
304 if (ctx->verbose) {
305 log_info("Add buffering to a potentially clock network '%s'\n", ctx->nameOf(ni));
306 }
307 }
308
309 // make new BUF cell single user for the net driver
310 IdString buf_name = ctx->idf("%s_BUFG", net.first.c_str(ctx));
311 ctx->createCell(buf_name, id_BUFG);
312 CellInfo *buf_ci = ctx->cells.at(buf_name).get();
313 buf_ci->addInput(id_I);
314 // move driver
315 CellInfo *driver_cell = ni->driver.cell;
316 IdString driver_port = ni->driver.port;
317
318 driver_cell->movePortTo(driver_port, buf_ci, id_O);
319 buf_ci->connectPorts(id_I, driver_cell, driver_port);
320 }
321 }
322
323 // =========================================
324 // Create DQCEs
325 // =========================================
326 void GowinPacker::pack_dqce(void)
327 {
328 log_info("Pack DQCE cells...\n");
329
330 // At the placement stage, nothing can be said definitively about DQCE,
331 // so we make user cells virtual but allocate all available bels by
332 // creating and placing cells - we will use some of them after, and
333 // delete the rest.
334 // We do this here because the decision about which physical DQCEs to
335 // use is made during routing, but some of the information (let’s say
336 // mapping cell pins -> bel pins) is filled in before routing.
337 bool grab_bels = false;
338 for (auto &cell : ctx->cells) {
339 auto &ci = *cell.second;
340 if (ci.type == id_DQCE) {
341 ci.pseudo_cell = std::make_unique<RegionPlug>(Loc(0, 0, 0));
342 grab_bels = true;
343 }
344 }
345 if (grab_bels) {
346 for (int i = 0; i < 32; ++i) {
347 BelId dqce_bel = gwu.get_dqce_bel(ctx->idf("SPINE%d", i));
348 if (dqce_bel != BelId()) {
349 IdString dqce_name = ctx->idf("$PACKER_DQCE_SPINE%d", i);
350 CellInfo *dqce = ctx->createCell(dqce_name, id_DQCE);
351 dqce->addInput(id_CE);
352 ctx->bindBel(dqce_bel, dqce, STRENGTH_LOCKED);
353 }
354 }
355 }
356 }
357
358 // =========================================
359 // Create DCSs
360 // =========================================
361 void GowinPacker::pack_dcs(void)
362 {
363 log_info("Pack DCS cells...\n");
364
365 // At the placement stage, nothing can be said definitively about DCS,
366 // so we make user cells virtual but allocate all available bels by
367 // creating and placing cells - we will use some of them after, and
368 // delete the rest.
369 // We do this here because the decision about which physical DCEs to
370 // use is made during routing, but some of the information (let’s say
371 // mapping cell pins -> bel pins) is filled in before routing.
372 bool grab_bels = false;
373 for (auto &cell : ctx->cells) {
374 auto &ci = *cell.second;
375 if (ci.type == id_DCS) {
376 ci.pseudo_cell = std::make_unique<RegionPlug>(Loc(0, 0, 0));
377 grab_bels = true;
378 }
379 }
380 if (grab_bels) {
381 for (int i = 0; i < 8; ++i) {
382 BelId dcs_bel = gwu.get_dcs_bel(ctx->idf("P%d%dA", 1 + (i % 4), 6 + (i >> 2)));
383 if (dcs_bel != BelId()) {
384 IdString dcs_name = ctx->idf("$PACKER_DCS_SPINE%d", 8 * (i % 4) + 6 + (i >> 2));
385 CellInfo *dcs = ctx->createCell(dcs_name, id_DCS);
386 ctx->copyBelPorts(dcs_name, dcs_bel);
387 ctx->bindBel(dcs_bel, dcs, STRENGTH_LOCKED);
388 }
389 }
390 }
391 }
392
393 // =========================================
394 // Create DHCENs
395 // =========================================
396 void GowinPacker::pack_dhcens(void)
397 {
398 // Allocate all available dhcen bels; we will find out which of them
399 // will actually be used during the routing process.
400 bool grab_bels = false;
401 for (auto &cell : ctx->cells) {
402 auto &ci = *cell.second;
403 if (ci.type == id_DHCEN) {
404 ci.pseudo_cell = std::make_unique<RegionPlug>(Loc(0, 0, 0));
405 grab_bels = true;
406 }
407 }
408 if (grab_bels) {
409 // sane message if new primitives are used with old bases
410 auto buckets = ctx->getBelBuckets();
411 NPNR_ASSERT_MSG(std::find(buckets.begin(), buckets.end(), id_DHCEN) != buckets.end(),
412 "There are no DHCEN bels to use.");
413 int i = 0;
414 for (auto &bel : ctx->getBelsInBucket(ctx->getBelBucketForCellType(id_DHCEN))) {
415 IdString dhcen_name = ctx->idf("$PACKER_DHCEN_%d", ++i);
416 CellInfo *dhcen = ctx->createCell(dhcen_name, id_DHCEN);
417 dhcen->addInput(id_CE);
418 ctx->bindBel(bel, dhcen, STRENGTH_LOCKED);
419 }
420 }
421 }
422
423 // =========================================
424 // Enable UserFlash
425 // =========================================
426 void GowinPacker::pack_userflash(bool have_emcu)
427 {
428 log_info("Pack UserFlash cells...\n");
429 std::vector<std::unique_ptr<CellInfo>> new_cells;
430
431 for (auto &cell : ctx->cells) {
432 auto &ci = *cell.second;
433 if (!is_userflash(&ci)) {
434 continue;
435 }
436
437 if (ci.type.in(id_FLASH96K, id_FLASH256K, id_FLASH608K)) {
438 // enable
439 ci.addInput(id_INUSEN);
440 ci.connectPort(id_INUSEN, ctx->nets.at(ctx->id("$PACKER_GND")).get());
441 }
442 gwu.remove_brackets(&ci);
443
444 if (have_emcu) {
445 continue;
446 }
447
448 // add invertor
449 int lut_idx = 0;
450 auto add_inv = [&](IdString port, PortType port_type) {
451 if (!gwu.port_used(&ci, port)) {
452 return;
453 }
454
455 std::unique_ptr<CellInfo> lut_cell =
456 gwu.create_cell(gwu.create_aux_name(ci.name, lut_idx, "_lut$"), id_LUT4);
457 new_cells.push_back(std::move(lut_cell));
458 CellInfo *lut = new_cells.back().get();
459 lut->addInput(id_I0);
460 lut->addOutput(id_F);
461 lut->setParam(id_INIT, 0x5555);
462 ++lut_idx;
463
464 if (port_type == PORT_IN) {
465 ci.movePortTo(port, lut, id_I0);
466 lut->connectPorts(id_F, &ci, port);
467 } else {
468 ci.movePortTo(port, lut, id_F);
469 ci.connectPorts(port, lut, id_I0);
470 }
471 };
472 for (auto pin : ci.ports) {
473 if (pin.second.type == PORT_OUT) {
474 add_inv(pin.first, PORT_OUT);
475 } else {
476 if (pin.first == id_INUSEN) {
477 continue;
478 }
479 if (ci.type == id_FLASH608K && pin.first.in(id_XADR0, id_XADR1, id_XADR2, id_XADR3, id_XADR4, id_XADR5,
480 id_XADR6, id_XADR7, id_XADR8)) {
481 continue;
482 }
483 add_inv(pin.first, PORT_IN);
484 }
485 }
486 }
487 for (auto &ncell : new_cells) {
488 ctx->cells[ncell->name] = std::move(ncell);
489 }
490 }
491
492 // =========================================
493 // Create EMCU
494 // =========================================
495 void GowinPacker::pack_emcu_and_flash(void)
496 {
497 log_info("Pack EMCU and UserFlash cells...\n");
498 std::vector<std::unique_ptr<CellInfo>> new_cells;
499
500 bool have_emcu = false;
501 for (auto &cell : ctx->cells) {
502 auto &ci = *cell.second;
503 if (!is_emcu(&ci)) {
504 continue;
505 }
506 have_emcu = true;
507
508 gwu.remove_brackets(&ci);
509
510 // The flash data bus is connected directly to the CPU so just disconnect these networks
511 // also other non-switched networks
512 ci.disconnectPort(ctx->id("DAPNTDOEN"));
513 ci.disconnectPort(ctx->id("DAPNTRST"));
514 ci.disconnectPort(ctx->id("DAPTDO"));
515 ci.disconnectPort(ctx->id("DAPTDI"));
516 ci.disconnectPort(ctx->id("TARGFLASH0HREADYMUX"));
517 ci.disconnectPort(ctx->id("TARGEXP0HAUSER"));
518 ci.disconnectPort(ctx->id("TARGFLASH0EXRESP"));
519 ci.disconnectPort(ctx->id("PORESETN"));
520 ci.disconnectPort(ctx->id("SYSRESETN"));
521 ci.disconnectPort(ctx->id("DAPSWDITMS"));
522 ci.disconnectPort(ctx->id("DAPSWCLKTCK"));
523 ci.disconnectPort(ctx->id("TPIUTRACECLK"));
524 for (int i = 0; i < 32; ++i) {
525 if (i < 4) {
526 if (i < 3) {
527 ci.disconnectPort(ctx->idf("TARGFLASH0HSIZE%d", i));
528 ci.disconnectPort(ctx->idf("TARGFLASH0HBURST%d", i));
529 ci.disconnectPort(ctx->idf("TARGFLASH0HRUSER%d", i));
530 ci.disconnectPort(ctx->idf("INITEXP0HRUSER%d", i));
531 }
532 // ci.disconnectPort(ctx->idf("TARGFLASH0HPROT%d", i));
533 ci.disconnectPort(ctx->idf("TARGEXP0HWUSER%d", i));
534 ci.disconnectPort(ctx->idf("MTXREMAP%d", i));
535 }
536 // ins
537 ci.disconnectPort(ctx->idf("TARGFLASH0HRDATA%d", i));
538 }
539 }
540 pack_userflash(have_emcu);
541 }
542
543 void GowinPacker::run(void)
544 {
545 handle_constants();
546 pack_iobs();
547 ctx->check();
548
549 pack_i3c();
550 ctx->check();
551
552 pack_mipi();
553 ctx->check();
554
555 pack_diff_iobs();
556 ctx->check();
557
558 pack_io_regs();
559 ctx->check();
560
561 pack_iodelay();
562 ctx->check();
563
564 pack_iem();
565 ctx->check();
566
567 pack_iologic();
568 ctx->check();
569
570 pack_io16();
571 ctx->check();
572
573 pack_gsr();
574 ctx->check();
575
576 pack_pincfg();
577 ctx->check();
578
579 pack_hclk();
580 ctx->check();
581
582 pack_dlldly();
583 ctx->check();
584
585 pack_bandgap();
586 ctx->check();
587
588 pack_wideluts();
589 ctx->check();
590
591 pack_alus();
592 ctx->check();
593
594 pack_ssram();
595 ctx->check();
596
597 constrain_lutffs();
598 ctx->check();
599
600 pack_pll();
601 ctx->check();
602
603 pack_adc();
604 ctx->check();
605
606 pack_bsram();
607 ctx->check();
608
609 pack_dsp();
610 ctx->check();
611
612 pack_inv();
613 ctx->check();
614
615 pack_buffered_nets();
616 ctx->check();
617
618 pack_emcu_and_flash();
619 ctx->check();
620
621 pack_dhcens();
622 ctx->check();
623
624 pack_dqce();
625 ctx->check();
626
627 pack_dcs();
628 ctx->check();
629
630 ctx->fixupHierarchy();
631 ctx->check();
632 }
633
634 void gowin_pack(Context *ctx)
635 {
636 GowinPacker packer(ctx);
637 packer.run();
638 }
639
640 NEXTPNR_NAMESPACE_END